Add DocIcon File Type to Content Query Web Part (CQWP) Result Set

Tools:  SharePoint Designer 2007

Business Challenge

This challenge involved a top-level site, multiple sub-sites and a common content type that was used across these sub-site document libraries to identify a specific type of document.  The structure resembled the following:

Top-Level Site
– Sub-Site #1
+ Document Library w/Policy Content Type
– Sub-Site #2
+ Document Library w/Policy Content Type
– Sub-Site #3
+ Document Library w/Policy Content Type

The end result is that all of the company Policies and Procedures that pertained to all employees would be easily accessed from a central place.  A few of the more technical requirements around this challenge involved the following
– a single page at the top-level site
– that displayed a list of documents
– that had a Content Type of Policy-Procedure
– and an Audience column value of All Employees

The top-level page would display the
– documents grouped by sub-site
– the name of the document hyperlinked to the specific document
– the file type icon (ie; Word, Excel, PPT, PDF)
– a short description of the document
– the name of the document author

Solution Option

Using the Content Query Web Part (CQWP) is only a partial answer because the out-of-the-box CQWP doesn’t support the requirements given above.  This is where a little bit of XSLT knowledge helps.  [SharePoint 2010 uses XSLT even more extensively so much so that XSLT is now the default way of showing information in lists, it will definitely help to make friends with XSLT.]


There are some common preparatory steps before playing with XSLT in 2007 CQWP’s:

  1. Create a Content Editor Web Part with the correct initial parameters.
  2. Identify the missing information and find out the internal name(s) of the column(s).
  3. Export the CQWP, open it in a text editor, and add the identified columns to the CommonViewFields node.
  4. Upload the modified CQWP into the Web Parts gallery, delete the original CQWP, and replace it with an instance of the modified CQWP.

To get you through this part, there is a step-by-step guide on the Microsoft ECM blog. Usually, the tricky part is finding the internal name of the field (column) to add. Once you’re past this stage, it’s time to open SharePoint Designer and do some XSLT’ing. Navigate to /Style Library/XSL Style Sheets and open ItemStyle.xsl. First, add this namespace to the root node (xsl:stylesheet):


You only need to do this once. Then, duplicate the Default template node (xsl:template) and give it an easily identifiable name without spaces. Also change the match attribute so that it contains the new name, in this pattern:


Now you’re all set for action.

Solution Details:  Display File Type Icons

With SharePoint Designer, edit the CQWP .webpart file, revise the CommonViewFields node of the Web Part to resemble the following:

The end result should look something like this:

<property name="CommonViewFields" type="string">File_x0020_Type, Text;
HTML_x0020_File_x0020_Type, Text;Policy_x0020_Description, Text;
Applies_x0020_To, Text

NOTE: The space between the column and the data type is important!

Open ItemStyle.xsl and create a new xsl:template node.

Adding these statements at the top of the template allows you to display all of the available field names that are available by default; it will also display the field names you supply in the CommonViewFields section above.

<xsl:for-each select="@*">
   P:<xsl:value-of select="name()" />

Next, add a new xsl:variable node as follows:

<xsl:variable name="FileTypeIcon">
  <xsl:value-of select="@File_x005F_x0020_Type" />

To present the icon, insert the following code immediately below <div id=”linkitem” class=”item”>:

  <a href="{$SafeLinkUrl}" target="{$LinkTarget}">
    <xsl:when test="$FileTypeIcon = 'pdf'">
      <img src="/_layouts/images/adobe.gif" alt="{@ImageUrlAltText}" />
      <img src="/_layouts/images/ic{@File_x005F_x0020_Type}.gif" 
      alt="{@ImageUrlAltText}" />

NOTE: The icon is placed to the left of the document title.

What this does is look up the file extension and load the corresponding icon image in the 12-hive.  Another option is to use the generic icon (icgen.gif).


That’s it! Save the XSL file and switch to the browser.

– Modify the Page Web Part
– Go to the Presentation section and expand it
– Apply the new item style of PolicyStyle

Work out the details between SharePoint Designer and the Page Web Part until you reach the final results that meet your requirements.


How has SharePoint affected you?


This entry was posted in SharePoint, SharePoint Designer. Bookmark the permalink.

2 Responses to Add DocIcon File Type to Content Query Web Part (CQWP) Result Set

  1. Claudette says:

    Thanks for some other informative website. Where else
    could I get that type of info written in such a perfect method?
    I’ve a undertaking that I am just now running on, and I’ve been at the look out for such information.

  2. It’s not my first time to visit this web page, i am browsing
    this site dailly and take nice data from here all the time.

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s