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. Christophe:

      Yes, that issue that you pointed to with attachments on forms is one that people ask about in the forums all the time. I know that there are a couple of posts in the MSDN SharePoint – Design and Customization (pre-SharePoint 2010) forum that explain how to fix it, too.

      I wonder if there are other things like this, as well. I did a little rooting around trying to figure out where the SharePoint:AttachmentsField is actually coming from in the hope of finding more capabilities, but I didn’t have any luck. Any ideas?

      M.

  1. In MOSS 2k7, I get the following error :
    Type ‘Microsoft.SharePoint.WebControls.AttachmentsField’ does not have a public property named ‘xmlns:SharePoint’.

    Any idea on how this could work with MOSS 2007?
    I don’t know if it makes a difference but I’m in a “publishing” page layout.

    1. When I did this, I was in WSS 3.0, so it’s not the version that’s the issue. I wasn’t in a publishing site, though.

      It sounds like you’re missing a namespace definition in your stylesheet. Can you post the whole XSL section of your DVWP?

      M.

      1. Hi. Here’s the declaration. The “xmlns:Sharepoint” is there, so I really don’t know what’s the error is all about.. I’ll make other tests next week and report the results.

  2. take 2 : <xsl:stylesheet xmlns:x=”http://www.w3.org/2001/XMLSchema” xmlns:d=”http://schemas.microsoft.com/sharepoint/dsp” version=”1.0″ exclude-result-prefixes=”xsl msxsl ddwrt” xmlns:ddwrt=”http://schemas.microsoft.com/WebParts/v2/DataView/runtime” xmlns:asp=”http://schemas.microsoft.com/ASPNET/20″ xmlns:__designer=”http://schemas.microsoft.com/WebParts/v2/DataView/designer” xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xmlns:msxsl=”urn:schemas-microsoft-com:xslt” xmlns:SharePoint=”Microsoft.SharePoint.WebControls” xmlns:ddwrt2=”urn:frontpage:internal”>

  3. This is cool, Marc!

    I’ve used it successfully in my SharePoint dataview. Now I’m looking for how to further customize the table generates. Say I wanted to parse out the file extentions or apply other custom formatting to the table. Do you have any idea how I’d go about that? I imagine I need to pass the to a but am at a loss how to get started.

    Thanks!
    Tim

    1. Tim:

      Since the control is rendering things, I don’t think you have much control over it. I’d probably write some script to rearrange things. jQuery is my favorite, of course.

      M.

    2. ACK! tags were removed from my post … let me retry:

      I’ve used it successfully in my SharePoint dataview. Now I’m looking for how to further customize the table SharePoint:AttachmentsField generates. Say I wanted to parse out the file extentions or apply other custom formatting to the table. Do you have any idea how I’d go about that? I imagine I need to pass the TABLE to a TEMPLATE but am at a loss how to get started.

      Thanks!
      Tim

  4. I have a situation here.
    When i try to add two DVWP on same .aspx page and both are having

    Then the attachments appears for the first DVWP only.
    If i remove the ‘SharePoint:AttachmentsField’ from the first DVWP. Then the attachments start appearing for the second DVWP.

    Can you figure out whats wrong ?

    Thanks.

    1. Amit:

      I don’t think that I’ve ever tried using this with more than one DVWP on the page. It sounds like there needs to be some sort of distinction between them. Sorry I don’t have a handy answer for you…

      M.

  5. I am using this, and I got it to work for the most part. However, I have editing enabled on the DVWP. When I view the page, and click to edit an item, and then save, I come back to the page and every item has the same attachments listed for it. If I leave the page and come back, though, it’s all back to normal. Any way around this behavior?

    Thanks!

    1. Cheyenne:

      Amit asked a similar question above. I just did a little Binging and found this post with a solution that’s supposed to work, but it doesn’t in my environment. Can you try it and let me know how it goes?

      M.

      1. I’ll be in a couple of your classes at SPTech Con in a couple of weeks – maybe I’ll bring my project along and show you the behavior?

        1. I’m hoping we can solve this before SPTechCon, but sure! It’ll be nice to meet you.

          I just spent some more time on this, and it’s a puzzler. I probably found all of the same articles you did, and I even saw a comment from you on one of them. There’s got to be something we’re missing that will get the context right for the SharePoint:AttachmentsField control.

          M.

  6. I am using Marc’s code in the following way. I have two DVWPs on a page (within a MOSS site), a master/detail scheme. My customer wants the attachments to show where by default all you see is ‘Yes’ or ‘No’.

    in the XSL code (picked up from the XSL editor under Modify Web Part), I simply replaced the word ‘Yes’ with Marc’s code block.

    Looks and works gorgeously. I haven’t a clue how it works.

Leave a Reply to Marc Cancel 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.