Friday, September 26, 2008

Getting the current position of a contentquery result

I am using a content query web part and want to have the first record appear differently than the other records that come after it. In order to do this you need something like the xsl position() function to determine if you are at the first element or not. You can't use position() from within your ItemStyles.xsl template though (it always returns a 1). So you need to make two quick changes to get this working.

First edit ContentQueryMain.xsl
  1. find the OuterTemplate.CallItemTemplate template
  2. Find this block:
    <xsl:when test="@Style='NewsCategoryItem'">
    <xsl:apply-templates select="." mode="itemstyle">
    [fix will go here]
  3. Add this to the spot I've indicated above:
    <xsl:with-param name="CurPos" select="$CurPosition" />
  4. Save ContentQueryMain.xsl

Now edit ItemStyles.xsl
  1. Add the following as the first item in your template:
    <xsl:param name="CurPos" />
Now you can refer to the $CurPos value whenever you need to do something different based on the position.

No comments: