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:value-of select=”substring($url_PATH_INFO, 2)” />
<xsl:value-of select=”substring-before($UrlRemoveSlash, ‘/’)” />
<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!
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.