For Attachments in a Data View

Note: Data Source must be SOAP.

1) Modify the DataSource QueryOptions parameters:
Create a Data View that pulls from a list via a data source. Add at least a title and attachment column to the view.

a. In the code, search for the line starting with:


On the same line you shall see something like:


b. Add the parameter requesting the attachments URL:


c. You should end up with this:


d. Save the page and check the result in the design view:

The ows_attachments field now contains the attachments URLs or 0 for list items with no attachments using the following format:


We still need to make some nice links out of this field.

2) Modify the XSL Presentation:
We first create a XSL template to transform the string given by the Web Service into a nice series of picture. You might use my sample below, paste this piece of code in the XSL part of the aspx page you are editing in SharePoint Designer (maybe just before the node “</xsl:stylesheet>”).

Note: Apply the following to the 2nd instance of “</xsl:stylesheet>” in your page (should be around the middle).

<xsl:template name="SplitAttachments">
<xsl:param name="str"/>
<xsl:when test="contains($str,';#')">
<xsl:variable name="attachmentUrl" select="substring-before($str,';#')"/>
<xsl:if test="string-length($attachmentUrl) != 0">
<a href="{$attachmentUrl}"><img style="border:0px" src="/_layouts/images/attach.gif" alt='Open'/></a>
<xsl:call-template name="SplitAttachments">
<xsl:with-param name="str" select="substring-after($str,';#')" />

This XSL template uses a recursive template to parse the attachments and generate corresponding links and pictures.

a. Now replace

<xsl:value-of select="@ows_Attachments"/>


<xsl:call-template name="SplitAttachments">
<xsl:with-param name="str" select="@ows_Attachments" />

And you should see this:

The paper clip icon you see are actually pointing to the attachments.

3) Display File Type Icons (instead of Paperclips):
The XSL extension provided by WSS (default prefix used by SharePoint Designer is DDWRT) can handle this through a MapToIcon template. If you give a file extension to this template, it will return the icon filename corresponding to this filetype.

Using the substring-after method to get the filetype we can imagine replacing:



/_layouts/images/{ddwrt:MapToIcon('', ddwrt:GetFileExtension(string($attachmentUrl)))}

4) Including a Tooltip description (Optional Hover over pop-up):
The full line of code, including a dynamic tooltip title lookup, is as follows:

<a target="_blank" title="Open: {substring-after((substring-after($attachmentUrl, 'Attachments')), '/')}" href="{$attachmentUrl}"><img style="border:0px" src="/_layouts/images/{ddwrt:MapToIcon('', ddwrt:GetFileExtension(string($attachmentUrl)))}" alt='Attachment'/></a>

a. Displaying FileType icons for files contained in a Doc Lib
If you're dealing with files other than attachments to a list item, you can modify your code as follows:

<img alt="Type" src="/_layouts/images/{ddwrt:MapToIcon('', ddwrt:GetFileExtension(string(@FileLeafRef)))}">

5) If you are trying to map an icon for a file extension that is not supported OOTB, you can add support for it in the "DOCICON.XML" file located in "C:/Program Files/Common Files/Microsoft Shared/web server extensions/14/TEMPLATE/XML" directory on the SharePoint Web server.  For example, to add support for VNC attachments, I first uploaded my vnc.png icon image to "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\IMAGES" directory, and then added the following line into the "DOCICON.XML" file referenced above, like so:

<Mapping Key="vnc" Value="vnc.png"/> 

Finally, to ensure any end user browser handles the file extension properly (i.e. not opening the file as text in the browser), you may want to add a MIME Type support for the extension in the IIS Manger, such as:

  • File name extension: .vnc
  • MIME Type: application/x-vnc

Once all of this is completed, iisreset for the changes to take effect.

Original(s): (more)