Wednesday, December 29, 2010

Anonymous user access CQWP

When trying to access Content Query Web Part with anonymous user, It failed to render. While it is working correctly with anonymous user.

I’ve found out that we can fix this issue by adding two fields to the CommonViewFields property of the CQWP:

DocumentIconImageUrl and OnClickForWebRendering, like this:

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

Other work around:

Create WebPart inherit the CQWP and add an extra column to the resulting table containing data:

public class AnonWorkingContentByQueryWebPart : ContentByQueryWebPart
protected override void OnInit(EventArgs e)
this.ProcessDataDelegate = ProcessItems;

protected virtual DataTable ProcessItems(DataTable data)
DataColumn column = new DataColumn("OnClickForWebRendering", typeof(string));

return data;

I prefer first solution

Webya give unauthorized error

When we added webya to our site it prompt with authentication. When we cancelled authentication page appear without any problem.

After investigation we found webya try to access /pages. To ignore this problem we added HTTPModule to escape this authetication. Sample Code as below:

public class BadRequestHandler : IHttpModule

/// Don't let the response be cached by the browser. Set up the status code
/// and return.

public void ProcessRequest(HttpApplication context)
if (context.Request.Url.AbsolutePath.ToLower().EndsWith("/pages/"))
ParseStatusCode(context, 200);


/// Actually set up the status code at this point, and return.

protected void ParseStatusCode(HttpContext context, int statusCode)
context.Response.StatusCode = 200;

public bool IsReusable
get { return true; }

public void Dispose()

public void Init(HttpApplication context)
context.BeginRequest += new EventHandler(context_BeginRequest);

void context_BeginRequest(object sender, EventArgs e)
//System.Web.HttpContext.Current.Response.Write("handler started");
if (System.Web.HttpContext.Current.Request.Url.AbsolutePath.ToLower().EndsWith ("/pages/"))
ParseStatusCode(System.Web.HttpContext.Current, 200);

Opening HTML Files in Sharepoint 2010

By default, Sharepoint 2010 is blocking the ability to open HTML, Flash, ...some types files directly from a document library. It download it instead of displaying it.

This can be changed in the browser file handling option. To change this option go to:

1. Central Admin\Application Management\Manage Web Applications.
2. Select General Settings for the specified web application
3. For Browser file handling, select permissive. The default is strict.

If we need to keep this option for security.
SharePoint 2010 put these types static in code, You need to create HTTPModule to remove attachment from its header. Example in code below:

public class FixHeaderModule : IHttpModule
public void Init(HttpApplication context)
context.PreSendRequestHeaders += OnPreSendRequestHeaders;

public void Dispose()


void OnPreSendRequestHeaders(object sender, EventArgs e)
// modify the "Server" Http Header
if (HttpContext.Current.Request.Url.AbsolutePath.EndsWith(".htm")

Tuesday, December 28, 2010

Add Additional Column to SharePoint 2010 Programmatically

When worked in project I faced problem in adding additional lookup column to list programmatically and I didn't found any article to guide me of doing it. After investigation I found the solution.
Example I have Lookup 2 lists:

  1. Countries with 2 text columns Title as key and Code.
  2. Application have lookup with name CountryTitle

I want to add Code Column of Countries list as additional column of look CountryTitle. The Code below do this action:

string mainListName = "Countries"; //Name of Lookup List
string lookupListName = "Application"; //Name of Main List Needs to add additional field to lookup
string lookupColAdditionalFieldDisplayName = "CountryCode"; //Display name of additional field in main list
string pLookupKeyName = "CountryTitle"; //Name of parent lookup field in main List
string additionalLookupFieldName = "Code"; //Name of additional Field in lookup list
//Main List
SPList mainlst = w.Lists[lookupListName];

//Lookup List
SPList lookuplst = w.Lists[mainListName];

//Main List Fields
SPFieldCollection col = mainlst.Fields;

//Lookup Primary Key
SPFieldLookup spPrimaryField = ((SPFieldLookup)col[pLookupKeyName]);

//Create Additional Field its name in lookup Field1 with display name in list TestDisplayName
SPFieldLookup field = (SPFieldLookup)mainlst.Fields.CreateNewField(SPFieldType.Lookup.ToString(), lookupColAdditionalFieldDisplayName);
field.LookupList = spPrimaryField.LookupList;
field.LookupWebId = spPrimaryField.LookupWebId;
field.LookupField = additionalLookupFieldName;
field.PrimaryFieldId = spPrimaryField.Id.ToString();
field.ReadOnlyField = true;
field.AllowMultipleValues = spPrimaryField.AllowMultipleValues;
field.UnlimitedLengthInDocumentLibrary = spPrimaryField.UnlimitedLengthInDocumentLibrary;
field.Direction = spPrimaryField.Direction;
SPAddFieldOptions op = SPAddFieldOptions.Default;

//Add Additional Field to main list
mainlst.Fields.AddFieldAsXml(field.SchemaXml, true, op);