Inconvenient provisioning of Content Query Web Part instances


Some time ago I wrote about provisioning instances of Content Query Web Part (CQWP) in a structured and repeatable way. Just recently I have found out that there is one very important thing you have to be aware of, if you don’t want to break the deployment process.

Back in June I provided you with a way to imitate the context required to provision a CQWP. Such context would consist of the minimal information required by the Content Query Web Part - the reference to the current site. In most scenarios the deployment process doesn’t end with deploying various CQWP instances. Further in the process you might want to create a new Publishing Page, Site or maybe even a whole new Site Collection.

Just recently I used Imtech SharePoint OneClickDeployment Studio to deploy a new MOSS Web Content Management (WCM) solution I’ve been working on for the last two weeks. That site was using quite a few instances of the Content Query Web Part which had to be deployed in a structured and repeatable way. During the deployment I started to get errors about having insufficient privileges and that while deploying as a domain admin! Eventually I discovered that the error had something to do with the context I was instantiating to provision CQWP.

It turns out that each time you instantiate SPSite the SharePoint Object Model reuses the context information if available. And if the context information you provided doesn’t contain any information on the current user, the created reference will be as it was called by an anonymous user! The HttpContext.Current property is static so it will remain available during the whole deployment process unless you explicitly clear it. Back in June I provided you the following code snippet to imitate the context data:

if (HttpContext.Current == null)
{
  HttpRequest request = new HttpRequest("", web.Url, "");
  HttpContext.Current = new HttpContext(request,
    new HttpResponse(new StringWriter()));
  HttpContext.Current.Items["HttpHandlerSPWeb"] = web;
}

After calling the snippet above you would be able to add instanced of CQWP to a page using the SPLimitedWebPartManager. From what I’ve just learnt, it is definitely a good practice to clear the context and not to run into troubles:

bool contextCreated = false;
if (HttpContext.Current == null)
{
    HttpRequest request = new HttpRequest("", web.Url, "");
    HttpContext.Current = new HttpContext(request,
        new HttpResponse(new StringWriter()));
    HttpContext.Current.Items["HttpHandlerSPWeb"] = web;
    contextCreated = true;
}

// instantiate CQWP here

if (contextCreated)
{
    HttpContext.Current = null;
}

Summary

Content Query Web Part provides a very powerful data aggregation solution. Thanks to its caching mechanism and great performance it is extremely useful especially in Web Content Management scenarios. After provisioning instances of CQWP in a structured and repeatable way it is important to clear the context information created for the purpose of deployment to prevent the SharePoint Object Model from using that information during the deployment.

Technorati Tags: SharePoint, SharePoint 2007, MOSS 2007, WCM

Others found also helpful: