Using a CQWP in a Master Page to Display “Site Contextual” List Items

What if you had an SharePoint site collection with many sub-site and in each sub-site (i.e. department) you wanted a news list. Now say you wanted a web part embedded in the master page to display the news items from the site that you were currently viewing. After a lot of trial and error (and some help from this post ) I finally figured out how to do this and I am ecstatic because this is certainly a requirement that comes up often (e.g. in the form of site contextual Quick Links etc).

So heres what you do:

Create your news lists on each sub-site. You can use the same name or unique names for the lists. Next, create a site content type to represent your news items (e.g. Dept News Item). Set the news lists to allow content types management and associate the new content type with each news list. Now add some test news items.

In your master page, add an extended cqwp (many to chose from) that allows you to use an alternative style sheet (xsl). Note: you could use the standard cqwp and modify the SharePoint ItemStyle.xsl but this isn’t recommended. Delete all the annoying <sample data> stuff and in the wp attributes delete WebUrl and add ContentTypeName and set this to the news content type.  In the wp <ParameterBindings> tags add the following:

<ParameterBinding Name=”url_PATH_INFO” Location=”ServerVariable(PATH_INFO)” DefaultValue=””/>

<ParameterBinding Name=”url_HTTP_HOST” Location=”ServerVariable(HTTP_HOST)” DefaultValue=””/>

 If you save the page and view it you should now see the web part displaying all news items from all sites.

Create a custom xsl style sheet and template in the site collection style library and add the following variable declarations at the start of your template:

<xsl:param name=”url_PATH_INFO” /><xsl:param name=”url_HTTP_HOST” />

You can now use these variables to determine which site you are currently in and based on this, filter the items displayed by the cqwp. Here is an example:

…..

<xsl:variable name=”CurrentPageUrl” select=”concat(‘http://’,$url_HTTP_HOST,$url_PATH_INFO)” />

  <xsl:variable name=”UrlRemoveSlash”>   

      <xsl:value-of select=”substring($url_PATH_INFO, 2)” />

     </xsl:variable>

    

     <xsl:variable name=”SiteName“>   

      <xsl:value-of select=”substring-before($UrlRemoveSlash, ‘/’)” />

     </xsl:variable> 

  <xsl:choose>

   <xsl:when test=”contains($SafeLinkUrl, $SiteName) “>

……

Finally modify the web part: ItemXslLinkOverride, ItemXslLink and <xsl:import href=..> to point to the new custom xsl style sheet and template and you are done!

cqwpnews2

cqwpconfig2

I’ve included some images (above) to demonstrate how the web part can be manually configured when added to a web part zone. You may prefer to configure it this way first and then when its all working, export the web part and add it to your master page.

Advertisements

One thought on “Using a CQWP in a Master Page to Display “Site Contextual” List Items

  1. Hi, im trying to filter my CQWP on a part of the actual URL. Maybe you can help me… I have a site called /project/ and from there many pages like /project/1/ ; /project/2/ ; /project/3/ etc… on the parent site /project/ i have a list like which has items in it and a column for the different projects 1,2,3 etc. Now i want to add a CQWP on the project subsites which only shows the entrys in the list which match the column 1,2 or 3 so on the page /project/1/ i want to see all entrys which have a 1 in the column, on /project/2/ only the one which have 2 in the column etc…

    Can you tell me how i can do this, espacially the xsl code? And were i have to put it…

    TY

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s