I’ve been playing with the CQWP (content query web part) quite a bit lately, customising it and trying to get just that little bit more out of it.
One of our clients wanted to show their news items in the following manner:
[Article title]
[Publishtype], [Publishdate]
[Article title] is the name of the news article, really just the page title. The [publishtype] is a choice field to assign each article a category and the [publishdate] is an extra date/time field to show when the actual article being referred to was published, as this isn’t always the same as the date the news article.
Creating the setup for the client was no problem. They were already using a publishing site, this was just a question of setting up a new content type based off of article page and adding the new site columns. The interesting bit is changing the content query so that it showed the articles in the way the client wanted on the front page of their site.
As usual, I used Heather Solomon’s excellent article on Customising the Content Query Web Part and Custom Item Styles to remind me of how to make the new site columns accessible to the CQWP and how to do the basic editing of the ItemStyles.xsl.
On a side note: I was able to call the [Publishdate] column with simply the “date” and not “datetime” type, e.g.
<property name="CommonViewFields" type="string">Publishtype, Choice;Publishdate, Date</property>
So, within the actual output block of the XSL template, you get something that looks approximately like this (apologies for the lack of tabs):
<div id=”linkitem”>
<xsl:call-template name=”OuterTemplate.CallPresenceStatusIconTemplate”/>
<div>
<a href=”{$SafeLinkUrl}” target=”{$LinkTarget}” title=”{@LinkToolTip}”>
<xsl:value-of select=”$DisplayTitle”/>
</a>
</div>
<div>
<xsl:value-of select=”@Publishtype” />,
<xsl:value-of select=”@Publishdate” />
</div>
</div>
There’s just one major problem with this. The title and publishtype look just fine. However, the date is output in the standard XSLT format. So instead of being pretty, 1 December 2009 looks like this:
2009-12-01 00:00:00
I couldn’t show the client that!
So, with some help from an example explaining Calendar Rollup from Today Forward with Custom Styles, I figured out how to change the date format. And this is where the interesting part of this article starts.
Step 1: add the ddwrt namespace
The ItemStyle.xsl needs to understand how to change the date for you. So you need to give it the right name space. Add the following line to the top of your ItemStyle.xsl, somewhere between the currently named namespaces:
xmlns:ddwrt=”http://schemas.microsoft.com/WebParts/v2/DataView/runtime”
Step 2: transform the date
SharePoint delivers the variable @Publishdate. This variable holds the date, in standard XSL format. What we need to do is make it look different, by using an XSL transform.
Add the following block of code just inside your XSL template block:
<xsl:variable name=”Publishdate”>
<xsl:value-of select=”ddwrt:FormatDateTime(string(@Publishdate),1033,
‘d MMMM, yyyy’)” />
</xsl:variable>
What we’re doing is creating a new XSL variable called $Publishdate; the $ shows up when we call it later. We fill the new XSL variable with the value of @Publishdate (remember, the original SharePoint variable), which we tweak a little bit. It’s a number of steps all in one go.
The bit about 1033 is the localisation. 1033 is English. 1043 is Dutch. And I found out by accident that 1034 is Spanish… This makes sure that, for example, the month is printed in the correct language for the user that visits the page. Need more localisations?
Finally, we decide how the date is formatted in the ‘d MMMM, yyyy’ bit. Basically the current date is shown as a digit – the 1st of December is shown as 1 and not 01. MMMM means the entire month and yyyy means the entire year, so 2009. The comma just puts a comma between the month and the year. For more information on formatting the date, see the XSL Transformations @ W3C on Formatting Dates and Times; you will scroll down a little bit.
Right, we have $Publishdate. What now?
Step 3: show the new Publishdate variable
In the block of HTML within your XSL template, I originally used:
<xsl:value-of select=”@Publishdate” />
You just change that to:
<xsl:value-of select=”$Publishdate” />
Yes, I really just changed the @ to a $ – by doing that we call the variable that was changed instead of the original SharePoint variable.
Summary
So, a quick overview of how that was done, after all that explanation:
1. Make sure your extra site columns are included in your CQWP
2. Add the namespace to ItemStyle.xsl
3. Add the transformation to the XSL block in ItemStyle.xsl
4. Call the new variable with a $ instead an @
