Aggregating content with the Content Query Web Part (CQWP)

In most web content management projects based on SharePoint the Content Query Web Part is used for aggregating content. Out of the box it have a few styles, but seems a little limited. With a little tweaking you can display the information you want in the way you want it.

There are mainly three parts involved here:

  1. The web part itself
  2. The ItemStyle.xsl
  3. The ContentQueryMain.xsl

The xsl files is defining how the content is displayed for the end users. The ItemStyle define the style for each item (like a row in a table), while the ContentQueryMain.xsl defines the surrounding (like the table around the rows). By exporting the web part you will find that there are plenty of properties not available in the user interface. The ones I change the most are:

CommonViewFields add columns that you can use in the web part.

<property name="CommonViewFields" type="string">Priority,Choice;PublishingStartDate,DateTime;Ingressbilde,Image;Ingress,HTML</property>

The Image field can be a little tricky

DataColumnRenames changes the name of the input column name to another name. Makes it easier to build the xsl when there are several fields that should be treated as they were the same. Here I rename Ingress to Description

<property name="DataColumnRenames" type="string">Ingress,Description</property>

ItemXslLink and MainXslLink can be pointed to your own XSL-stylesheet for how you want to display the items and what styles you want to make available for the users.

<property name="ItemXslLink" type="string" >/Style Library/XSL Style Sheets/MyItemStyle.xsl</property>

<property name="MainXslLink" type="string">/Style Library/XSL Style Sheets/MyContentQueryMain.xsl</property>

Update: Remember that both ItemXslLink and MainXslLink is server relative. So if your site collection is in the managed path /Sites/ then the xsl-links must reflect that. So for a Site Collection https://example.com/sites/department you will have this property

<property name="ItemXslLink" type="string" > /sites/department/ Style Library/XSL Style Sheets/MyItemStyle.xsl</property>

Update end

To set the default style you set the ItemStyle property:

<property name="ItemStyle" type="string">MyNewsArchive</property>

 

Inside the ItemStyle.xsl you can add several styles. I like to add one that displays all data (columns) that is available to use. This view is developer friendly, but very user unfriendly.

<xsl:template name="ShowXML" match="Row[@Style='ShowXML']" mode="itemstyle">
    <xsl:for-each select="@*">
        <br />
        Name: <xsl:value-of select="name()" />
        <br />
        Value:<xsl:value-of select="." />
    </xsl:for-each>
</xsl:template>

 

You can also use the core search results web part to aggregate content, but then based on a search. The customizing of the results is much of the same as with CQWP, but handled a little different. See more about customizing search here

 

Resources

How to: customize the Content Query Web Part by using Custom Properties (MSDN)

Configuring and Customizing the Content Query Web Part (MS ECM team blog)

Customizing the Content Query Web Part and Custom Item Styles (Heather Solomon blog)

The Enhanced Content Query Web Part (CodePlex)