This is something I have been wanting for a while and I finally managed to get it right :)

Scenario:
You want to be able to create a default publishing page that contains a pre-configured content query web part that rolls up information from the current web location. This is can be usefull for instance if you would like to display a summary rollup of specific pages within the site and mabye even have a list of all the pages in the site as a navigational item on each content page.

Problem:
The problem with the standard content query web part is that a user has to manually set the web location of the content that they want rolled up. If you try add it to a layout page then each page created will always show content from the web location that was defined / all sites in the site collection, regardless of where in the hierarchy the page is sitting.

Solution:
I created a custom content query web part called "What Web Content Query" (hey, I am no good at naming things, we all know this) that inherits from the default content query web part, but overrides the "WebUrl" setting with the site path in the current URL. You will not believe how handy this has come in :)

Build It:
So, here is how you build it:

  • Open Visual Studio 2005 / 2008
  • Create New Project -> C# -> SharePoint -> WebPart
  • Add a new reference to "Microsoft.SharePoint.Publishing.dll"
  • Use my code sample below to modify the contents of your .cs file (the important stuff is in orange):

using Microsoft.SharePoint.Publishing.WebControls; //Need this reference for the ContentByQuery web part

namespace WhatWebCQWP
{
    [Guid("03b0b57c-6607-44f5-adfd-b361d6129687")]
    public class WhatWebCQWP : ContentByQueryWebPart
    {
        public WhatWebCQWP()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        protected override void CreateChildControls()
        {
            Uri WebLocation = new Uri(SPContext.Current.Web.Url);
//Replace the default web location property of CQWP with current URL
            this.WebUrl = WebLocation.AbsolutePath.Replace("%20", " "); //Get only the absolute path and replace encoded space with unencoded space
            base.CreateChildControls();
        }

        protected override void RenderWebPart(HtmlTextWriter output)
        {
            base.RenderWebPart(output);
        }
    }
}

Once you have built the web part and deployed it to your farm you may want to know how to add it to your layout page:

  • Create a new layout page.

  • In SPD, drag the web part onto yout layout page (I would also recommend that you add the web part itself outside of a web part zone template, like the Summary Links web part)

  • Save and create a page based on your new layout somewhere.

Sweet. Now I have not gone in to heavy detail, this should be enough for all the smart people out there to follow :) What you may want to do is look at including custom item styles and specific fields that you want displayed - go HERE: http://www.heathersolomon.com/blog/articles/CustomItemStyle.aspx

 

Regards,

 

Doug "bobthebuilder" McCusker