Document Icon in CQWP

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”>
<xsl:variable name=”SafeLinkUrl”><xsl:call-template name=”OuterTemplate.GetSafeLink”>
<xsl:with-param name=”UrlColumnName” select=”‘LinkUrl'”/>

<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:variable name=”LinkTarget”>
<xsl:if test=”@OpenInNewWindow = ‘True'” >_blank</xsl:if>

<div >
<xsl:when test=”@DocIcon=””>
<img src=”_layouts/images/folder.gif” alt=”” />
<img src=”_layouts/images/ic{@DocIcon}.gif” alt=”” />
<xsl:call-template name=”OuterTemplate.CallPresenceStatusIconTemplate”/>
<a href=”{$SafeLinkUrl}” target=”{$LinkTarget}” title=”{@LinkToolTip}” style=”padding-left:5px;”>
<xsl:value-of select=”$DisplayTitle”/>

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