Search This Blog

Monday, August 17, 2009

Add ..more hyperlink to Announcement Webpart


Description: I was having a request from end user to display ..more hyperlink the annoucement webpart, which redirects to respective item display page.

Solution: Create a custom XSL template for Content Query Webpart. Then use Content Query Webpart to display Announcement content using Custom XSL template. Follow these steps:

1. Open root site of web, go to View All Site Content---> Style Library -->XSL Style Sheets & locate the file ItemStyle.xsl. Open ItemStyle.xsl with any text editor & insert following template named AnnouncementStyle at the bottom before </xsl:stylesheet> tag :

<xsl:template name="AnnouncementStyle" match="Row[@Style='AnnouncementStyle']" 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 id="linkitem" class="item">

<div class="link-item">
<NOBR><div><a href="{$SafeLinkUrl}" title="{@LinkToolTip}">
<xsl:value-of select="$DisplayTitle"/>
</a></div>
<div class="description"> By <xsl:value-of select="@Author"> </xsl:value-of> <xsl:value-of select="ddwrt:FormatDate(string(@Created), 1033, 2)"> </xsl:value-of></div></NOBR>
<div class="description">
<xsl:value-of select="substring(@Body,1,200)" disable-output-escaping="yes"/>
<a href="{$SafeLinkUrl}" title="Read more.."> ...more
</a>

</div>
</div>
</div>
</xsl:template>

2. Add a Content Query Webpart on the page where you want to display the announcement & set Query-->Source to Show items from the following list: & Select the Announcement List you want to display. Now, export the webpart to .webpart file on your PC. Open exported .webaprt file & locate the tag <property name="CommonViewFields" type="string">
replace above line with
<property name="CommonViewFields" type="string" >Body, text; Created, text;FileDirRef,text </property>

3. Now, import above modified .webpart file & add this CQW webpart to page & remove previously added CQW.
4. Modify above CQ webpart & set the Presentation-->Item Style to AnnouncementStyle.

Result: Finally CQW should display announcements like following: