Tuesday, March 11, 2008

How to display all fields available in the content query web part (CQWP)

When working with customizing the Content Query Web Part (aka CQWP), you often want to know what you're dealing with. In this article I'm going to show you how you can show all the fields and their contents for all the items being displayed.

There are several suggestions out there on how to accomplish this, but here's my take. This recipe involves editing the style sheets (XSLT) for the CQWP. The CQWP uses three different style sheets for rendering the content set up to be displayed. By default these three are:

  1. /Style Library/XSL Style Sheets/ContentQueryMain.xsl
  2. /Style Library/XSL Style Sheets/Header.xsl
  3. /Style Library/XSL Style Sheets/ItemStyle.xsl
The one we are going to focus on, is the last one: ItemStyle.xsl.

Using SharePoint Designer, open this file. Add the following template text in the bottom of the file (but before the closing tag):

<xsl:template name="ShowXML" match="Row[@Style='ShowXML']" mode="itemstyle">
<xsl:variable name="SafeLinkUrl">
<xsl:call-template name="OuterTemplate.GetSafeLink">
<xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
Item: <i>
<a href="{@LinkUrl}">
<xsl:value-of select="@Title" />
<xsl:for-each select="@*">
<xsl:sort select="name()"/>
<xsl:value-of select="name()" />
<xsl:text disable-output-escaping="yes"> </xsl:text>
<xsl:value-of select="." />
<br />

  • Save the file, and do a Check-in.
  • Now go back to your page containing your CQWP, Edit Page, and choose Modify Shared Web Part on the CQWP.
  • Under the Category Presentation, in the Item Styles dropdown list, now choose your new style called ShowXML. See screen shot below

  • Finally, click Apply or OK.
The CQWP will now display all the items with all the fields and their content. I've formatted the output somewhat, but as you can see from the code pasted into ItemStyle.xsl earlier, it's easy to make changes.
For example of result, see screen shot below.

That's it! There's nothing to it!

But, the point of doing this in the first place is not only to see which fields you have available. You have probably created your own fields you want to use in the CQWP. So, to make them available here, I would like to send you over to Heather Solomons' excellent article on how to accomplish this. Check it out here: Customizing the Content Query Web Part and Custom Item Styles

For more information on how to customize the CQWP and its style sheets, check out my de.icio.us bookmarks on http://feeds.delicious.com/rss/frankove/cqwp. I won't bother posting all the links in this post, since I constantly find new good links. But here are a few good ones:

1 comment:

mnateras said...

Hello Frank,

I have a particular problem regarding the content query webpart, let me know if you can help me out

I want to implement the CQWP to retrieve the last 5 Post of 3 different discussion boards, I have already placed the query correctly and I have modified the ItemStyle.xsl in order to display some more information, the tricky thing is that among this extra fields to display I want to display the # OF REPLIES PER DISCUSSION, and I just havent found out the way to pull this info, seams there is no variable or column with this data.

Thanks for you support