by Andreas

Sharepoint – use XSLT to define first element in CQWP

In a recent project I had the need for two Content Query Web Parts where one displayed the first 3 items from a list in a specific format, while the other had to display the remaining rows (from 4 and onwards) from the same list in a different format. Strangely enough I couldn’t find much useful information about this, but the solution was easy enough.

PS: For reasons not worth explaining, I solved this using two web parts but this could quickly be altered to be handled in a single CQWP.

The approach is to get the current row number, compare it to a value and decide whether or not to display the result. This was all done with XSLT in Sharepoint Designer.

1. Get the current row number

<xsl:variable name="RowNum" select="count(./preceding-sibling::*)" />

2. Add a simple IF statement to check if row number is higher than 3

<xsl:if test="$RowNum &gt; 3">
    <!-- whatever is in here will be rendered -->

I chose to use this to remove the first 3 elements of the result, but it can also be used to apply one style to the first X elements of a result and another style for the remaining elements.

There you have it, conditional filtering solved in Sharepoint Designer with OOTB functionality.