Friday, March 14, 2008

Editing XSL Style Sheets - beware!

Made a discovery yesterday afternoon.
I have made some changes to my default XSL Style Sheet, ItemStyle.xsl. I'm gonna be honest, I'm no style sheet guru! But I have managed to create a few methods for formatting things, for instance formatting and displaying a date. (I found tips for this on this article.)

At first I had these methods in ItemStyle.xsl, but then I saw that these came up as alternatives in the ContentQueryWebPart Item Style dropdown list. So I thought that I would put them in the Main style sheet, the ContentQueryMain.xsl. I also changed the method names, to more alike the ones that are already there, for instance "OuterTemplate.FormatDate". And it worked! Beautiful!

But on some of sites, the SummeryLinkWebPart has been used. This is a pretty straight-forward "display my links" kind of web part. For some reason, this web part now didn't work as it should. It displayed the following error:

"Unable to display this Web Part. To troubleshoot the problem, open this Web page in a Windows SharePoint Services-compatible HTML editor such as Microsoft Office SharePoint Designer. If the problem persists, contact your Web server administrator."
Solution:
After much error searching inside the definition of this web part in my site definition, I found out that the custom methods I created in ContentQueryMain.xsl, also had to be added to a style sheet file called SummaryLinkMain.xsl. This file also resides in /Style Library/XSL Style Sheets/, along with the style sheets for the ContentQueryWebPart and the RSS web part.

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'"/>
</xsl:call-template>
</xsl:variable>
<b>
Item: <i>
<a href="{@LinkUrl}">
<xsl:value-of select="@Title" />
</a>
</i>:
</b>
<ol>
<xsl:for-each select="@*">
<xsl:sort select="name()"/>
<li>
<xsl:value-of select="name()" />
<xsl:text disable-output-escaping="yes"> </xsl:text>
<i>
<xsl:value-of select="." />
</i>
</li>
</xsl:for-each>
</ol>
<br />
</xsl:template>

  • 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: