Intranoggin

Blither, Blather, Web Content Management.
Blog » CQWP: 30 Tips #10 Passing Current Position

CQWP: 30 Tips #10 Passing Current Position

It can often be handy within your item style template to know what row you’re processing.  For example, if you are displaying items in a table format and you want to spit out a row of column headers before the item data if you’re processing the first row.

Making this information available to your item style template is just a couple quick updates.  First, inside ContentQueryMain.xsl, update OuterTemplate.CallItemTemplate to send along the current position as a parameter.

<xsl:template name=”OuterTemplate.CallItemTemplate”>
        <xsl:param name=”CurPosition” />
        <xsl:value-of disable-output-escaping=”yes” select=”$BeginListItem” />
        <xsl:choose>
            <xsl:when test=”@Style=’NewsRollUpItem’”>
                <xsl:apply-templates select=”.” mode=”itemstyle”>
                    <xsl:with-param name=”EditMode” select=”$cbq_iseditmode” />
                </xsl:apply-templates>
            </xsl:when>
            <xsl:when test=”@Style=’NewsBigItem’”>
                <xsl:apply-templates select=”.” mode=”itemstyle”>
                    <xsl:with-param name=”CurPos” select=”$CurPosition” />
                </xsl:apply-templates>
            </xsl:when>
            <xsl:when test=”@Style=’NewsCategoryItem’”>
                <xsl:apply-templates select=”.” mode=”itemstyle”>
                    <xsl:with-param name=”CurPos” select=”$CurPosition” />
                </xsl:apply-templates>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates select=”.” mode=”itemstyle”>
<xsl:with-param name=”CurPos” select=”$CurPosition” />
                </xsl:apply-templates>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:value-of disable-output-escaping=”yes” select=”$EndListItem” />
    </xsl:template>

Note that by adding that update, you’ll always send across the current position even if the receiving item style template doesn’t use or declare a CurPos parameter.

Now, whenever you want to use the current position in your item style template, just declare the CurPos parameter and use it.

</xsl:template><xsl:template name=”TableLayout” match=”Row[@Style='TableLayout']” mode=”itemstyle”>
<xsl:param name=”CurPos”/>
      <xsl:variable name=”SafeImageUrl”>
          <xsl:call-template name=”OuterTemplate.GetSafeStaticUrl”>
              <xsl:with-param name=”UrlColumnName” select=”‘ImageUrl’”/>
          </xsl:call-template>
      </xsl:variable>
<xsl:if test=”$CurPos = ’1′”>
          <tr>
              <td>Column Header 1</td>
              <td>Column Header 2</td>
              <td>Column Header 3</td>
              <td>Column Header 4</td>
          </tr>
      </xsl:if>
      <tr>
          <td><xsl:value-of select=”@Column1″/></td>
          <td><xsl:value-of select=”@Column2″/></td>
          <td><xsl:value-of select=”@Column3″/></td>
          <td><xsl:value-of select=”@Column4″/></td>
      </tr>
  </xsl:template>


Posted: 2/16/2012 4:42:00 AM by Ryan Miller | with 0 comments
Filed under: SharePoint