sharepointxsltblogscqwp

Counting top 5 most-commted posts in a SharePoint blog using XSL


I'm basically editing ItemStyle.xsl to get it to retrieve what I need and display through a CQWP (Content Query Web Part). I need it to display only the top 5 posts with the most comments. The comments can be retrieved via @NumComments. I'm not familiar enough with XSL to know how to do it, I'm assuming using count? Any tips?

Here's the current XSL code for that template which just displays all posts.

<xsl:template name="MostCommented" match="Row[@Style='MostCommented']" 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>
        <a href="{$SafeLinkUrl}" target="{$LinkTarget}" title="{@LinkToolTip}">
            <xsl:value-of select="$DisplayTitle"/>
        </a>
    </div>
</xsl:template>

Solution

  • I was able to do this directly in the CQWP web part instead of the ItemStyle.xsl

    Replacing

    <property name="QueryOverride" type="string" />
    

    With

    <property name="QueryOverride" type="string"><![CDATA[<OrderBy><FieldRef Name="NumComments" Nullable="True" Type="Lookup" Ascending="False"/></OrderBy>]]></property>