Wednesday, May 21, 2008

Content Query Web Part - Custom Item Style

  1. Open SharePoint Designer and go to the site Style Library
  2. Open the file: ItemStyle.xml
  3. Copy the following code or one of the existing styles from the XML
  4. Modify the XML as required; for example the code below places an image on the left of a title, in this case used for portal news headlines.
  5. Check in the XML file
  6. Refresh the page you are editing the CQWP on and open the Presentation section of the CQWP edit panel. The new style should now be listed under Item Style.













<xsl:template name="NewsHeadline" match="Row[@Style='NewsHeadline']" 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="SafeImageUrl">
<xsl:call-template name="OuterTemplate.GetSafeStaticUrl">
<xsl:with-param name="UrlColumnName" select="'ImageUrl'"/>
</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>
<xsl:variable name="LinkTarget">
<xsl:if test="@OpenInNewWindow = 'True'" >_blank</xsl:if>
</xsl:variable>
<div id="linkitem" class="item">
<xsl:if test="string-length($SafeImageUrl) != 0">
<div class="image-area-left">
<a href="{$SafeLinkUrl}" target="{$LinkTarget}">
<img class="image" src="{$SafeImageUrl}" alt="{@ImageUrlAltText}" />
</a>
</div>
</xsl:if>
<div class="link-item">
<xsl:call-template name="OuterTemplate.CallPresenceStatusIconTemplate"/>
<strong><a href="{$SafeLinkUrl}" target="{$LinkTarget}" title="{@LinkToolTip}">
<xsl:value-of select="$DisplayTitle"/>
</a></strong>
</div>
</div>
</xsl:template>

1 comment:

  1. Thanks alot for such a useful post. I have a query here. I have created a number of very similar templates as above for differnt pages of my site. Some of the fields in some templates have specific format to be displayed in like 'Updated' text should appear before the Modified field value. Now i want to generalise my XSl i.e. i want to create a single template which i can use across multiple pages. I want to make sure that the only the fields for which i enter the column name should be displayed, others should not. Is its possible? If yes, please provide a detailed solution.

    ReplyDelete