Wednesday, February 18, 2009

Show document icon using XSL and DDWRT


When displaying a document icon using XSL you may have tried to just map the image from the /_layouts/images folder using the file extension. This will work in most cases but it will not map PDF files etc. unless you add the images or edit the file mappings.

The ddwrt library has just this function made for you:

include a reference to the library in your xslt:

xmlns:ddwrt="http://schemas.microsoft.com/WebParts/v2/DataView/runtime"

This is a snippet that includes the icon and sets the correct icon if the “file” is a folder:

<a href="{@FileRef}">
<xsl:choose>
<xsl:when test="@FSObjType=1"> <!-- Folder -->
<img alt="{@Title}" src="/_layouts/images/folder.gif" style="vertical-align: middle;border:none;"/>
</xsl:when>
<xsl:otherwise>
<img alt="{@Title}" src="/_layouts/images/{ddwrt:MapToIcon('', string(@File_x0020_Type))}" style="vertical-align: middle;border:none;"/>
</xsl:otherwise>
</xsl:choose>
</a>




Folders presented in xsl 

1 comment:

Chris H. said...

Thank you so much, that's exactly what I was looking for all day!!