SharePoint Customization: Show document icons in a data-view web part

Great tip from Mauro Cardarelli on showing document icons in a data-view. I use data-views a lot as well and can definately say they are one of the number one reasons *NOT* to be afraid of FrontPage.

I’m a big fan of the Data View web part.  I like how I can create one document library then generates “dynamic” pages based on document attriubutes.  I do this using the Data View web part (per page) and Filter conditions.  It’s very easy to create simple HTML tables with document information and the great thing is they require no mantenance.  When a document is added to the repository that matches the filter conditions, it automatically shows up on the right page.

One of the challenges with using the Data View web part with documents is the quirkiness of some of the display options.  I wanted to show the document icon (pdf, doc, etc.) next to the document title and have it associated with a hyperlink to the actual content.  Unfortunately, while the Data View web part has a property called ‘File Type’, it is text only.  So, you are left with a text-based value for the file type and a separate field for the actual document link.  Well, I got the functionality I wanted with a little XSL manipulation!  Details are below…

Note, the Data View web part requires FrontPage 2003 usage.  There has been a lot of talk about FP2003 and unghosting.  It is possible to follow the steps below then export and publish the customized web part (through a temporary page) so it can be used on standard pages. 

1. Collect your document icons.  I created an image library with pdf, doc, xls and ppt icons.  You could refer to the actual images on the file system but I wanted a little more control over what I am showing.

2. Drop a Data View web part on a page and connect it to a SharePoint document library

3. Select the DVWP and choose ‘Data View properties…’ from the right-click menu

4. Drag ‘File Type’ and other attributes you wish to show onto the table

5. Select the ‘File Type’ attribute in the table and click ‘Create’ – ‘Apply Formatting…’ on the right task pane

6. Create some conditional formatting rules like ‘File Type equals doc’ then select Format and choose Font; make it red

7. Go to the Code tab and look for your conditional code.  It should look like this (minus the comments indicator)…

<!– <xsl:if xmlns:xsl="http://www.w3.org/1999/XSL/Transform" test="normalize-space(@File_x0020_Type) = ‘doc’">color: #FF0000;</xsl:if> –>

8. Replace it with this (minus the comments indicator)…

<!– <xsl:if xmlns:xsl="http://www.w3.org/1999/XSL/Transform" test="normalize-space(@File_x0020_Type) = ‘pdf’"><a target="_blank"><xsl:attribute name="href"><xsl:value-of xmlns:xsl="http://www.w3.org/1999/XSL/Transform" select="@FileRef"/></xsl:attribute><img border="0" src="/Repository/My Images/icon_pdf.gif" width="16" height="16"/></a></xsl:if>
<xsl:if xmlns:xsl="http://www.w3.org/1999/XSL/Transform" test="normalize-space(@File_x0020_Type) = ‘doc’"><a target="_blank"><xsl:attribute name="href"><xsl:value-of xmlns:xsl="http://www.w3.org/1999/XSL/Transform" select="@FileRef"/></xsl:attribute><img border="0" src="/Repository/My Images/icon_word.gif" width="16" height="16"/></a></xsl:if>
<xsl:if xmlns:xsl="http://www.w3.org/1999/XSL/Transform" test="normalize-space(@File_x0020_Type) = ‘xls’"><a target="_blank"><xsl:attribute name="href"><xsl:value-of xmlns:xsl="http://www.w3.org/1999/XSL/Transform" select="@FileRef"/></xsl:attribute><img border="0" src="/Repository/My Images/icon_excel.gif" width="16" height="16"/></a></xsl:if>
<xsl:if xmlns:xsl="http://www.w3.org/1999/XSL/Transform" test="normalize-space(@File_x0020_Type) = ‘ppt’"><a target="_blank"><xsl:attribute name="href"><xsl:value-of xmlns:xsl="http://www.w3.org/1999/XSL/Transform" select="@FileRef"/></xsl:attribute><img border="0" src="/Repository/My Images/icon_ppt.gif" width="16" height="16"/></a></xsl:if> –>

9. That’s it!  The icon should now shows up in your table and should open a new browser session to your document.

You may also like...

1 Response

  1. You could also use the Quick Tag Selector to add an image using HTML to an empty column, and use this as the HTML:

    <img src=”/_layouts/images/ic{@File_x0020_Type}.gif”>

    This would work if all your document types have standard icons in the images directory.

I would love to hear from you.

%d bloggers like this: