Using SharePoint:AttachmentsField in a DVWP’s XSL

Here’s proof that you should never stop learning (or at least trying to learn).  I ran across a post on the MSDN SharePoint – Design and Customization forum just now that showed me a trick I’d never run into before. I don’t know that I’ve ever had the need to show file attachments for items in a SharePoint list using a Data View Web Part (DVWP) before, but this is a really nice trick to do it.

You can add the following chunk of XSL into the dvt_1.rowview template (or anywhere else where you are in the context of a single node):

<xsl:element name="SharePoint:AttachmentsField">
	<xsl:attribute name="runat">server</xsl:attribute>
	<xsl:attribute name="FieldName">Attachments</xsl:attribute>
	<xsl:attribute name="ControlMode">Display</xsl:attribute>
	<xsl:attribute name="Visible">true</xsl:attribute>
	<xsl:attribute name="ItemId">
		<xsl:value-of select="@ID"/>
	</xsl:attribute>
</xsl:element>

Here’s a rather ugly, simple little example, but it should get the point across. Note that the item with the Title = “Tue Aug 25 11:52:42 CDT 2009” has two attachments, and both are shown.  The attachment names are links that allow you to open the attachments, too.

image

Now that I know the trick, some Binging shows me that lots of others have known all along. See, keep learning!