Monday, October 8, 2012

CQWP XSL customizations - Read More Option

CQWP XSL customizations  - Read More Option

Posted by Nadeem Yousuf | 05:13 | , | 0 comments » Content Query webpart series:
Part 1: Configuring SharePoint 2010 CQWP

Part 2: CQWP PageQueryString and PageFieldValue
Part 3: CQWP XSL customizations
Part 4 : CQWP QueryOverride
CQWP style sheets are present inside a folder "XSL Style Sheets" in Style Library at the root site collection. ItemStyle.xsl file contains the style templates which can applied to the items returned by CQWP. The best practice to create custom styles is to make the copy of  ItemStyle.xsl and make changes in it and then upload the new customized xsl file to the style library.
Now let's create a custom style which will show the Title of our Company Announcements in bold font and show  first 200 characters of the Body field followed by ...Read More link. Create a copy ItemStyle.xsl file and name it  CustomItemStyle.xsl file. Open the file in any editor and paste the following at the end of the file before </xsl:stylesheet>. Upload the CustomItemStyle.xsl to Style Library inside XSL Style Sheets folder.


<xsl:template name="CustomStyle" match="Row[@Style='CustomStyle']" mode="itemstyle">
              <xsl:variable name="SafeLinkUrl">
                     <xsl:call-template name="OuterTemplate.GetSafeLink">
                           <xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>
                           <xsl:with-param name="UseFileName" select="1"/>
                     </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>
              <p>
                     <strong>
                           <xsl:value-of select="$DisplayTitle"/>
                     </strong><br />
                     <xsl:value-of select="substring(@Body,0,200)" disable-output-escaping="yes"/>
                     <a href="{$SafeLinkUrl}">...Read more</a>
              </p>

       </xsl:template>

Now add a fresh CQWP to your page and then export it. Save the generated file and open it in notepad. Locate property ItemXslLink and replace it with following:
<property name="ItemXslLink" type="string">/Style Library/XSL Style Sheets/CustomItemStyle.xsl</property>
Locate property ItemStyle and replace it with following:
<property name="ItemStyle" type="string">DetailedOverview</property>
Save the webpart file and upload it to the SharePoint page.
After you upload the webpart file, you can find the webpart in Imported Web Parts category. Now add the web part to the page and edit it. Make the webpart to read data from whole site collection, select Announcements List, select My Content Types Group and Company Announcements content type. 
In the presentation section select following:
As can be seen a field named Body has been added. This is called a Slot. If we want to show more fields we can add them in xsl as we added Body field. Click OK to save the settings. The final result will be something like this.


No comments:

Post a Comment