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:

SharePoint:SoapDataSource

On the same line you shall see something like:

</listName></GetListItems></soap:Body>

b. Add the parameter requesting the attachments URL:

<queryOptions><QueryOptions><IncludeAttachmentUrls>TRUE</IncludeAttachmentUrls></QueryOptions></queryOptions>

c. You should end up with this:

</listName><queryOptions><QueryOptions><IncludeAttachmentUrls>TRUE</IncludeAttachmentUrls></QueryOptions></queryOptions></GetListItems></soap:Body>

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:

;#http://moss/Lists/MyList/Attachments/1/Piece.txt;#http://moss/Lists/...

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:choose>
<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:if>
<xsl:call-template name="SplitAttachments">
<xsl:with-param name="str" select="substring-after($str,';#')" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

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"/>

With

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

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/attach.gif

With

/_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):
blog.jonathanroussel.com
blog.jonathanroussel.com (more)
sympmarc.com
msdn.microsoft.com