Document Icon in CQWP

September 10, 2007

After the (deserved) summer vacation period, I resumed the "normal" work schedule.

One of the first issues I had to solve was how to display the document icon in a Content Query Web Part (CQWP) query results. Fortunately, I stumbled upon a post in Russian by Tatiana that explained the process.

First, we should add the DocIcon in the CommonViewFields property (export the CQWP, edit the .webpart file) as follows:

<property name="CommonViewFields" type="string">DocIcon, Lookup;</property>

Then, we edit the Style Library/XSL Style Sheets/ItemStyle.xsl file in SharePoint Designer to add a new template (named "DocLib" in this example):

<xsl:template name="DocLib" match="Row\[@Style='DocLib'\]" mode="itemstyle">  
<p>  
<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>  
<xsl:variable name="LinkTarget">  
<xsl:if test="@OpenInNewWindow = 'True'" >\_blank</xsl:if>  
</xsl:variable>  
  
<div >  
<xsl:choose>  
<xsl:when test="@DocIcon=''">  
<img src="\_layouts/images/folder.gif" alt="" />  
</xsl:when>  
<xsl:otherwise>  
<img src="[\_layouts/images/ic{@DocIcon}.gif](mailto:_layouts/images/ic%7B@DocIcon%7D.gif)" alt="" />  
</xsl:otherwise>  
</xsl:choose>  
<xsl:call-template name="OuterTemplate.CallPresenceStatusIconTemplate"/>  
<a href="{$SafeLinkUrl}" target="{$LinkTarget}" title="[{@LinkToolTip](mailto:%7B@LinkToolTip)}" style="padding-left:5px;">  
<xsl:value-of select="$DisplayTitle"/>  
</a>  
</div>  
</p>  
</xsl:template>

PS: Sorry for the horrible formatting of the XML, my CopyTextAsHTML VS2005 add-in is having some difficulties with the leading < characters.


Profile picture

Written by Edin Kapić Insatiably curious code-writing tinkerer. Geek father. Aviation enthusiast. Cuisine journeyman. Follow me on Twitter