Add ItemStyle template to Display thumbnail in Content Query webpart

In SharePoint 2007, the OOTB content query webpart is unable to display the image thumbnail from Picture Library or Picture Column (in custom list).

To display the thumbnail in content query web part, we need to create new template and add it to ItemStyle.xsl (…/Style Library/XSL Style Sheets/)

<!-- Picture Library - Thumbnail and Title -->
<xsl:template name="Thumbnail-Title" mode="itemstyle" match="Row[@Style='Thumbnail-Title']">
<xsl:variable name="SafeImageUrl">
<xsl:call-template name="OuterTemplate.GetSafeStaticUrl">
<xsl:with-param name="UrlColumnName" select="'ImageUrl'"></xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="SafeLinkUrl">
<xsl:call-template name="OuterTemplate.GetSafeLink">
<xsl:with-param name="UrlColumnName" select="'LinkUrl'"></xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="Description">
<xsl:call-template name="OuterTemplate.GetTitle">
<xsl:with-param name="UrlColumnName" select="'LinkUrl'"></xsl:with-param>
</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>
<xsl:with-param name="UrlColumnName" select="'LinkUrl'"></xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="LinkTarget">
<xsl:if test="@OpenInNewWindow = 'True'">_blank</xsl:if>
</xsl:variable>
<xsl:variable name="FileRef">
   <xsl:call-template name="CustomTemplate.GetFilename">
      <xsl:with-param name="url" select="@LinkUrl"></xsl:with-param>
   </xsl:call-template>
</xsl:variable>
<div class="item" id="linkitem">
<table>
<tr>
<td>
<img>
<!-- NOTE: add img tag for picture presentation, and assign pic url to its src attribute -->
<xsl:attribute name="src">
    <xsl:value-of select="concat(substring-before(@LinkUrl,$FileRef),'_t/',substring-before($FileRef,'.'),'_',substring-after($FileRef,'.'),'.',substring-after($FileRef,'.'))"></xsl:value-of>
</xsl:attribute>
<!-- NOTE: this command will return the thumbnail url
<xsl:value-of select="@LinkUrl" /> will display the original image -->
</img>
</td>
<td valign="top">
<xsl:value-of select="$DisplayTitle"></xsl:value-of>
</td>
</tr>
</table>
</div>
</xsl:template>

<!-- Helper Template to get the file name of images -->

<xsl:template name="CustomTemplate.GetFilename"> 
   <xsl:param name="url"></xsl:param> 
   <xsl:choose> 
      <xsl:when test="contains($url,'/')"> 
         <xsl:call-template name="CustomTemplate.GetFilename"> 
         <xsl:with-param name="url" select="substring-after($url,'/')"></xsl:with-param> 
         </xsl:call-template> 
      </xsl:when> 
      <xsl:otherwise> 
         <xsl:value-of select="$url"></xsl:value-of> 
      </xsl:otherwise> 
   </xsl:choose> 
</xsl:template>

The template above is only suitable for Picture Library. To display Picture column in custom list, we have to add another template.

Since there are only some common view fields are passed into content query webpart, we need to add the picture column to our webpart. In this example, we suppose the picture column name is “image”.

<property name=”CommonViewFieldstype=”string“>image,URL</property>

NOTE: In SharePoint 2010, we will be able to add additional field through Tool Part Pane. However, in SP2007, we have to subclass content query webpart and extend its ToolPartPane to make it possible.

<!-- Custom List with Picture column -->
<xsl:template name="Thumbnail" mode="itemstyle" match="Row[@Style='Thumbnail']">
<xsl:variable name="SafeImageUrl">
<xsl:call-template name="OuterTemplate.GetSafeStaticUrl">
<xsl:with-param name="UrlColumnName" select="'ImageUrl'"></xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="SafeLinkUrl">
<xsl:call-template name="OuterTemplate.GetSafeLink">
<xsl:with-param name="UrlColumnName" select="'LinkUrl'"></xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="Description">
<xsl:call-template name="OuterTemplate.GetTitle">
<xsl:with-param name="UrlColumnName" select="'LinkUrl'"></xsl:with-param>
</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>
<xsl:with-param name="UrlColumnName" select="'LinkUrl'"></xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="LinkTarget">
<xsl:if test="@OpenInNewWindow = 'True'">_blank</xsl:if>
</xsl:variable>
<!-- Get the file name from image column -->
<xsl:variable name="FileRef">
   <xsl:call-template name="CustomTemplate.GetFilename">
      <xsl:with-param name="url" select="substring-before(@image,',')"></xsl:with-param>
   </xsl:call-template>
</xsl:variable>
<div class="item" id="linkitem">
<table>
<tr>
<td>
<img>
   <xsl:attribute name="src">
      <xsl:value-of select="concat(substring-before(@image,$FileRef),'_t/',substring-before($FileRef,'.'),'_',substring-after($FileRef,'.'),'.',substring-after($FileRef,'.'))" />
   </xsl:attribute>
</img>
</td>
<td valign="top">
<xsl:value-of select="$DisplayTitle"></xsl:value-of>
</td>
<td>
<xsl:value-of select="@image" />
</td>
</tr>
</table>
</div>
</xsl:template>
Advertisements

2 thoughts on “Add ItemStyle template to Display thumbnail in Content Query webpart

  1. great post! i am just starting out in community management marketing media and trying to learn how to do it well – resources like this article are incredibly helpfulhttp://www.busquemail.com.br

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s