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!

Similar Posts

87 Comments

  1. Hi Marc,
    I’m trying to access a list on a root site from a subsite and I did that by creating a web part page and inserting DVWP on the subsite of the root list. I have the attachment problem though where the list item shows either Yes or No whether the item has an attachment. When I replace the code with what’s shown on this page i get this error when refreshing the page.

    “An unexpected error has occurred.

    Web Parts Maintenance Page: If you have permission, you can use this page to temporarily close Web Parts or remove personal settings. For more information, contact your site administrator. ”

    I’ve tried everything that’s on the web, but I can never get a link of the attachments to show successfully from the subsite. Do you know how I can fix this problem?

    Thanks

      1. David:

        Consider using client-side script and SPServices to grab the attachment info. It can often be cleaner and it’s not that “expensive” if you aren’t showing a huge number of items in the view.

        M.

  2. Thank you so much! Its cool…..actually I had come across a similar solution but it was using list id. Though I enetred correct list id still it was giving me an error. Your solution worked without any issue.

  3. Hey thanks for that! I just wanted to leave one missing bit to get this to work: Don’t forget to import the `SharePoint` namespace:

    `xmlns:SharePoint=”Microsoft.SharePoint.WebControls”`

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.