Archive

Archive for the ‘CQWP’ Category

Site Navigation with a Content Query Web Part in a SharePoint Page Layout

November 1, 2011 Leave a comment

A brief on one of my current projects was to create left hand navigation for an new Intranet which was built up from the Pages library.  As is usual with Intranets, the content was spread across multiple publishing sites under a single Site Collection, hence each site had it’s own Pages library.  It’s a simple task to set this up for each page using the Content Query Web Part (CQWP) but ideally this should be part of a Page Layout that can be used on every site.  

The other requirement was that the Title of the Web Part should be that of the current site, so Information Technology if the user was view pages from that site, Operations for the Operations site etc.

Out of the box, this would not be possible as the CQWP is linked to a single library and it’s Title is set from the Web Part Properties. You obviously could add a CQWP to each page but this is inefficient and beyond most content editing users. So for a more elegant solution you’ll need to use Visual Studio to create your own Web Part, however the code is extremely straightforward and is derived from ContentByQueryWebPart. You will need to include a reference to Microsoft.SharePoint.Publishing.WebControls in your project which is in the Microsoft.SharePoint.Publishing namespace and the code below should be enough to get you up and running.

public class ContentNavigation : ContentByQueryWebPart
{
 protected override void CreateChildControls()
 {
   SPWeb web = SPContext.Current.Web;
   SPList list = web.GetList(web.Url + "/Pages");
   this.ListGuid = list.ID.ToString();
   this.ListName = list.Title;
   base.CreateChildControls();
 }

 protected override void OnPreRender(EventArgs e)
 {
   this.Title = SPContext.Current.Web.Title;
   base.OnPreRender(e);
 }
}

Once packaged and deployed, it’s then straightforward to create a Page Layout containing this web part.

Advertisements
%d bloggers like this: