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

      Good question. Do you want to display an icon (like the paperclip) if there are 1-n attachments, the icon for the particular filetypes, or something else? The basic idea would be to adapt the xsl to emit the markup you want.

      M.

      1. Hi Marc,

        First of all, love the blog. It’s one of my ‘go-to’ resources for specifically DVWP stuff.

        If Patrick’s question is anything like mine (which brought me to this thread in the first place) is that if the user attaches a .jpg or any other sort of image as the attachment, is it possible to display that image using the value displayed here. Obviously we can’t just wrap an IMG tag around it, but I’m thinking there must be a way to assign this value to a variable and wrap an img tag around that…

        Cheers,
        Kelvin

        1. Kelvin:

          I’m glad you find my blog helpful!

          With a little script in the page, you could pretty easily convert the image links to img tags on page load. That’s probably the easiest route.

          M.

  1. Hello Marc…Love your posts…I tried this functionality and am getting the following error:
    Error Creating Control: SharePoint:Attachmentsfield Object reference not set to an instance of an object. Any clue on what I would be missing. The whole list renders fine, except the Attachments Link column.

    Any help would be greatly appreciated. Thanks.

      1. I have a list, and displaying on a page using DVWP. As mentioned in your post, I am putting the code inside tag, but it is not recognizing the ‘SharePoint:Attachmentsfield’ object. Do I need to register anything on the page?

        Let me know what additional info I can provide to clarify. Thanks for a quick response.

      2. Hello Marc

        Here is what I have:
        An annoucements list with attachments. I am using a DVWP to display the details. The attachments column details is stored under the following tag: , as mentioned in your post.

        Do I need to register the before using ‘SharePoint:Attachmentsfield’ object somewhere on the page?

        Thanks for a quick response.

  2. Hi Marc,
    I am having MOSS 2007 environment and using a custom list with DVWP. I have successfully integrated attachment feature in my custom Edit Page, I am not using NewForm.aspx page for new item rather I am using DVWP inert template option in which a link is added to current view. In this view I am not able to find any way to implement attachment feature. When I goes for new record it show ” No ” under attachment column. How I can achieve desire functionality. Thanks in advance.

    1. Nadeem:

      If you’re talking about inline editing in a DVWP, I don’t think there’s a way to do what you want without some custom coding. You *might* be able to get something to work by AJAXing in the attachment controls from another page, but I’m guessing that the forms digest will get in your way.

      M.

      1. Thanks Marc,

        May be I am not able to explain you clearly. For my custom Edit form attachment I am using XSLT based code. Can I use same code for inline editing as you mentioned above, sorry for bothering you again.

  3. Hi Marc,

    I know this is an old post, but I keep running across this post and your post on “Using a DataSource in a Data View Web Part (DVWP) in a Different Site in SharePoint Designer 2010” in trying to find an answer to my question. I’ve gotten code from this post to show attachments in a DVWP at a site collection level. When using the same code at a site level to access a list at the site collection level using code from your other post, everything works perfectly except that no attachments appear. Are you aware of any settings that would prevent attachments in a list at the site collection level to not appear at the site level in a DVWP?

    Ken

    1. Ken:

      I can’t think of any reason it wouldn’t work, but I don’t think I’ve ever done it. There’s a comment below from Dan Blaker that will solve it, I think.

      When using this in a data view using an aggregate data source (i.e. joined lists), I had to specify the ListID for the attachments control by including the following XSL between the xsl:element tags:

      <xsl:attribute name="ListId">
         <xsl:value-of xsl:select="$ListID" />
       </xsl:attribute>
      

      M.

      1. Hi Marc,

        Thank you for the quick reply and suggestion! I added the ListID attribute to the attachment control, however the DVWP continues to return ‘1’ for the @Attachments property but politely ignores displaying the attachments using the code above at the site level. I’ll continue searching for an answer and will post back here if I find one. I’m thinking, however, that I may have to unfortunately resort to separate lists at the site level (thankfully I only have three sites within the collection).

        Thanks again,

        Ken

  4. Hi Marc – thanks for the solution above. I’ve successfully used both the long version you have here and the shorter version (condensing it all into a single shorthand tag) multiple times, but now I’m running into an issue I’ve never seen before.

    I did a quick DVWP at the root level of my site which shows list items and the attachments on each item. Everything works great at the root location, but when I move it down one layer into a sub-site, the attachments column shows as blank. There isn’t any kind of error, and the list items themselves are showing up fine. It’s just that the attachment names disappear and I get a blank column for that part. Just like I always do, I added the “WebURL” parameter and changed the ViewFlag before moving it. It seems tied to the location of the DVWP page, but since the list items are showing up the problem ONLY seems to be with the attachment values.

    Have you seen anything like that before?

    1. UPDATE: My issue sounds like what Ken Bowman described in his original post. Per your reply to him, I tried adding the ListID attribute to the attachments element, but it had no visible effect.

      1. I’m not sure what the issue is, frankly. I’m not sure if I’ve ever tried this in different subsites. Is the list itself in the root site and you’re trying to display it in the subsite? If so, that may be the issue.

        M.

  5. Not sure what was causing the issue. I had built the webpart at the root level where the list is, then uploaded it into the Web Part Gallery and was putting it on the subsite that way. I did a line-by-line comparison between the two (working version at root level and broken version at subsite level) but couldn’t find a difference. I opened both in Designer and copied & pasted the entire DVWP, and after that it started working. It was the first time I’ve seen that before, but apparently there was some difference I wasn’t seeing during the comparison.

    1. I always copy and paste the DVWP code from page to page. I’ve never had much luck with using the Web Part Gallery for DVWPs. SharePoint does some manipulation when you save the DVWP into a .webpart file, perhaps stripping required pieces out.

      M.

  6. Hi
    I am getting “System.NullReferenceException” when i add this code in xslt of content query webpart, I have included SharePoint namespace as well on the top of xslt.

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.