Home > CQWP, Publishing, SharePoint, Web Part > Site Navigation with a Content Query Web Part in a SharePoint Page Layout

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

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;

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

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

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: