If you have ever messed around with the XSL Itemstyles for use with the CQWP in SharePoint, you know how valuable they can be. Also how frustrating they can be from time to time.

This is not as tricky as it may sounds. Out of the box, the Content Query WebPart (CQWP) does not support group by date. What you need to understand, is how the Header.xsl work, when you group. The trick is to group by a simple text-column but display the date-column.

I am creating a Logbook, which for some reason needs to use the “Group by date” using the CQWP. Hey.. I needed a case for this, so why not?

The 4 steps

  1. Create a list with a calculated column for the date
  2. Create a new Header style in header.xsl
  3. Add a Content Query WebPart
  4. Fix the description (optional)

Step 1: Create a list with a calculated column for the date

Create a list with the following columns:

    • Title (Single line of text)
    • Description (Multiple lines of text)
    • ModifiedGroup (Calculated)
    • ModifiedDisplay (Calculated)
        In the ModifiedCreate-column you format the Created-column (or any other date column you are messing around with) so it looks like this:

ModifiedGroup

To get a nicely formatted date for my grouping, I have created a ModifiedDisplay-column. This column displays the date in a Danish format. The calculated column looks like this:
ModifiedDisplay

Step 2: Create a new Header style in header.xsl

Open SharePoint Designer and navigate to “/Style Library/XSL Style Sheets/Header.xsl”. Edit the file in advanced mode.

Copy/paste in the xsl-code below.

<xsl:template name="Logbook" match="*[@GroupStyle='Logbook']" mode="header"><div class="groupheader item medium"><xsl:value-of select="@ModifiedDisplay" /></div></xsl:template>

Step 3: Add a Content Query WebPart

Find the page you are using to disply your Logbook or what else you are working on. My query looks like this:

My presentation looks like this. The important part is to choose to sort by ModifiedDisplay. Otherwise, you do not get the nice title.

The end result should look a lot like this:

Step 4: Fix the description

The clever reader has already noticed that something is wrong with the output of the description field. To fix this, navigate to “/Style Library/XSL Style Sheets/ItemStyle.xsl”. Edit the file in advanced mode.

Create a new template for use with your Logbook. Never edit any of the standard ones. It is just good practice, not to mess to much with Microsofts code. It is better to add more, that to replace the existing.

Feel free to copy/paste my edition, which is very close to the original, but note the line for the @Description. It now reads:

 <xsl:value-of select="@Description" disable-output-escaping="yes" />

It is the disable-output-escaping=”yes” that makes all the difference.

Remember to edit your CQWP to use the “Logbook” Item style.

Here is my XSL-code for getting the correct display:

<xsl:template name="Logbook" match="Row[@Style='Logbook']" mode="itemstyle"><xsl:variable name="SafeLinkUrl"><xsl:call-template name="OuterTemplate.GetSafeLink"><xsl:with-param name="UrlColumnName" select="'LinkUrl'"/></xsl:call-template></xsl:variable><xsl:variable name="DisplayTitle"><xsl:call-template name="OuterTemplate.GetTitle"><xsl:with-param name="Title" select="@Title"/><xsl:with-param name="UrlColumnName" select="'LinkUrl'"/></xsl:call-template></xsl:variable><div class="item"><div class="link-item"><xsl:call-template name="OuterTemplate.CallPresenceStatusIconTemplate"/><a href="{$SafeLinkUrl}" title="{@LinkToolTip}"><xsl:if test="$ItemsHaveStreams = 'True'"><xsl:attribute name="onclick"><xsl:value-of select="@OnClickForWebRendering"/></xsl:attribute></xsl:if><xsl:if test="$ItemsHaveStreams != 'True' and @OpenInNewWindow = 'True'"><xsl:attribute name="onclick"><xsl:value-of disable-output-escaping="yes" select="$OnClickTargetAttribute"/></xsl:attribute></xsl:if><xsl:value-of select="$DisplayTitle"/></a><div class="description"><xsl:value-of select="@Description" disable-output-escaping="yes" /></div></div></div></xsl:template>

That gives me this display

No more steps.

Tweet me

  • Learn how to edit XSL Itemstyles in order to “Group by Date” in SharePoint 2010 – Click to Tweet
  • SharePoint Group By Date using the CQWP in 4 Simple Steps Click to Tweet


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.