Thursday, May 07, 2009

MOSS 2007 - CQWP: Group Header Count

In a recent change request, I was asked to change a CQWP to output a hypterlink to "top of page" to the right of the group heading if it was any heading other than the first. Considering a list of items grouped by year, the output should look something like this:

2009
    Item 1
    Item 2
    Item 3
2008        (top of page)
    Item 1
    Item 2
2007        (top of page)
    Item 1
    Item 2
...


If you've used the CQWP at all, you'll know all the output formatting templates are xml files stored in the directory "/Style Library/XSL Style Sheets/" which you can access via SPD.

The various style formats for group headings are stored in header.xsl, but all the "looping" is done a level higher in ContentQueryMain.xsl. You'll need to edit both files to make this change. In my example, luckily, I didnt need to keep count of every unique group name. I only needed to know if I was on the first group heading.

So, the first thing I do is edit ContentQueryMain.xsl to pass along a variable to tell header.xsl if we are on the first group.



You can see in the screenshot, all I'm doing is adding a new hard coded parameter to the CallHeaderTemplate call where MS already has code to do special output if the current row is the first group header row. Piggybacking off of that code, I can pass this parameter down to the header.xsl template as shown below.



That gets the variable to the header.xsl template. Now we need to edit that file and catch the passed parameter, then use that value to test and see if we are outputting the first group header, and if so, we dont display the new hyperlink.



The only two parts you need from the above screenshot is this variable param

<xsl:param name="GroupPos">

and also the test for anything but a value of 1

<xsl:if test="$GroupPos != 1" >
    <div style="float:right;">
        <a href="#top">top of page</a>
    </div>
</xsl:if>


All done! now just edit a CQWP instance to use the new group heading style and tweak to your liking.

The next step would be to track the group headings with a counter, possibly to alter the template for every other header row...

No comments: