Thursday, October 7, 2010

Anonymous access, CQWP and picture libraries

I had used a picture library to store the information to be displayed using a content query web part. It worked fine for authenticated users, but failed when the site was configured for anonymous access. The content query web part failed with the following error, when viewed by anonymous users:

Error while executing web part: System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.SharePoint.Publishing.WebControls.ContentByQueryWebPart.SetDocumentIconUrlAndOnClickString(SPWeb web, DataRow row, String strDefaultItemOpen, Boolean fSetDocIcon, Boolean fSetOnClick, String fileRefColumnRef, String progIdColumnRef, String fsobjTypeColumnRef, String permMaskColumnRef)..


Resolution:

Investigating the method, SetDocumentIconUrlAndOnClickString using reflector, I found this line of code which would result in this exception for anonymous users:

builder.Append(StrJScriptParameter(Convert.ToString(web.CurrentUser.ID, CultureInfo.InvariantCulture), true));

When I was about to override this behavior by code, I found a more elegant non-code solution on the web. The solution was to export the web part and add the following to the commonviewfields property:

<property name="CommonViewFields" type="string">DocumentIconImageUrl;OnClickForWebRendering</property>

This solved the problem.

No comments:

Post a Comment