Content Query Web Part: SharePoint's Swiss Army Knife

SharePoint's Content Query Web Part (CQWP) is the Swiss Army Knife of SharePoint web parts. Not only is it good at efficiently aggregating data from a variety of locations within a site collection, but you can exert a great deal of creative control over how it renders its results.

We most normally enhance CQWP output by changing ItemStyle.xsl, which is a located in the site collections root folder as shown:

Figure 1: ItemStyle.xsl.

One typically changes ItemStyle.xsl to renders results in a table format, to add additional metadata to the listing or change fonts and colors. These techniques are well known and understood (check them out here: http://www.heathersolomon.com/blog/articles/CustomItemStyle.aspx or here: http://paulgalvin.spaces.live.com/blog/cns!1CC1EDB3DAA9B8AA!491.entry).

Business Problem

That's powerful stuff but the range of opportunity to customize is more or less scoped to the individual items that the CQWP finds. ItemStyle modifications affect individual items. Many times that's good enough but what happens when you want a higher level customization? This article describes how you can use CQWP customization features to create a nice web browser navigation for long listings. Consider this business scenario:
    * You have a document library with 100 or more documents that contain white papers.

    * You want to display all of these on one page and style the output to show title and description.

    * You've defined some metadata for these documents (beyond the default title, author, description, etc). Specifically, your white papers have a "domain" metadata field. "Domain" includes values such as "Application Development" and "Quality Control".

    * Your 100 documents are spread more or less evenly across eight domains.

    * You want to use CQWP to display all 100 white papers grouped by domain and sorted by title.
All of those bullet points are easily met using CQWP out of the box. Use the techniques described in the Solomon article to customize look and feel and you're on your way. See Figure 2 for an example:

Figure 2: Using CQWP Default Features.

There is a problem with a pure out-of-the-box approach, however. The web browser will scroll when you show 100 documents and that's an unfriendly interface.

Solution

There's an easy answer to this problem. Broadly speaking, we'll do the following:
    * Modify the XSL style sheet that emits the Group By headers ("Application Life Cycle Management" and "Customer Support" in Figure 2) to emit anchor tags.

    * Add a content editor web part on the same page that references those displayed group headers.

Modify ContentQueryMain.xsl

First, let's edit the CQWP's display function for group headers. The file, ContentQueryMain.xsl, is located in the style library (see Figure 1 above). Open it with SharePoint designer and jump to the OutTemplate.GetGroupName section (near the end). Make a change as show in future 3:

Figure 3: Modifying ContentQueryMain.xsl.

That most interesting and key bit of XSL is the following:
    * <a id="{$GroupName}" title="{$GroupName}" href="#{$GroupName}">: Emits the group name as an anchor tab on the page.
This change causes CQWP to render the group name as an anchor tag on the page rather than just a text label.

Add a Menu

To close the loop on this, we need a navigation panel. Simply add a content editor web part on your page and create a series of links such as:
    * <a href="#Application Life Cycle Management">Application Life Cycle Management</a><P></P> * <a href="#Customer Support">Customer Support</a>

    * Add one of these for each grouping that you need
Tidy it up with some descriptive text and you're done.

Conclusion

CQWP is a versatile web part indeed. Microsoft's architecture allows us to change the look and feel of individual items and even show entirely separate metadata via ItemStyle.xsl modifications. Take it one step further and higher and modify the enclosing ContentQueryMain.xsl to decorate the group names with anchor tags in order to enable intra page navigation. Couple that with Content Editor Web Part and you can overlay a nice navigational system on top of it.
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Networking Solutions

Partners

More Networking