Tuesday, April 28, 2009

Separate XSL files for Content Query Webpart (CQWP)

Using the same ItemStyle.xsl for all the content query webparts in the site is a very bad thing to do, especially with the big sites.
But you can make an XSL file for each content query webpart and here is how to do:

  1. Edit the page you want to add the webpart in.
  2. Add a content query webpart.
  3. Export the webpart.
  4. Open the exported file.
  5. Search for “ItemXslLink”
  6. Update the line to:

    <property name="ItemXslLink" type="string">/Styles/XSL/MyCQWebpart.xsl</property>

  7. Update the property "ItemStyle" to the templete name ("UsefulLinks" in this example)
  8. Upload the XSL file (remember to update "CommonViewFields" as needed)
  9. The XSL file can be something like this to display a useful links (Title, Link):

    <xsl:stylesheet version="1.0" exclude-result-prefixes="x d xsl msxsl cmswrt" xmlns:x="http://www.w3.org/2001/XMLSchema" xmlns:d="http://schemas.microsoft.com/sharepoint/dsp" xmlns:cmswrt="http://schemas.microsoft.com/WebParts/v3/Publishing/runtime" xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
    <xsl:template name="UsefulLinks" match="Row[@Style='UsefulLinks']" mode="itemstyle">
    <xsl:param name="CurPos" />
    <xsl:variable name="TitleLenght" select="string-length(@Title)" />
    <xsl:variable name="RowLimit" select="40" />
    <xsl:variable name="LinkURL" select="substring-before(@Link,',')" />
    <xsl:variable name="LinkText" select="substring-after(@Link,',')" />
    <li>
    <a href="{$LinkURL}" target="_blank" title="{$LinkText}" class="newsTitle">
    <xsl:value-of select="$CurPos" /> - <xsl:value-of select="@Title" />
    </a>
    </li>
    </xsl:template>
    </xsl:stylesheet>

  10. Import the webpart and configure it to read from the right place, filtration and sorting order and so on.
  11. Go to the “ContentQueryMain.xsl” file and search for “OuterTemplate.CallItemTemplate”
  12. In <xsl:apply-templates select="." mode="itemstyle"> add <xsl:with-param name="CurPos" select="$CurPosition" /> and any other parameters you want to send to the itemstyle.
  13. You can also make your own ContentQueryMain XSL by updating the “MainXslLink” property in the webpart file, and the same for the header by updating the “HeaderXslLink” property.

No comments:

Blessings of working on large real-world projects

Working on large real-world projects can offer numerous blessings and benefits, both professionally and personally. Here are some of the key...