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 tried using this on a DVWP on MOSS 2007, in the
    Rowview template. I am getting the below error: An error occurred
    during the processing of . Cannot create an object of type
    ‘System.Int32’ from its string representation ” for the ‘ItemId’
    property. Any help on correcting this error. Regards
    -Padmaja

    1. Hi Padmaja I was getting the same error. Do one thing Go to design view->click on addnew column to the lft/right. Once the column is added just write down any value in the cell in design view itself. It will reflect in all the rows(you need to have 5-10 items in list).Then go to code view and search for that value (like I entered sagar and searched for sagar). Replace that value with the piece of code mentioned above in this article without missing or changing a single word. It should work for you

  2. Hi Marc and everyone. thisis great I have the same requirement in a dvwp as Michael. I just can’t figure out where in the code this snippet goes. Does it replace the code part that shows my attachment field? I tried that but it just messes up the page.

    Any help would be appreciated.

    jm

    1. John:

      It all depends on what you’d like to show, of course, but generally (as I point out in the post) you’d add this snippet into the dvt_1.rowview template.

      M.

  3. 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>

      1. I have an aggregate source and added Dan’s code. I get an error – a string literal was expected. Do I need to set up a ListID in the parameter binding; How would it go?

        1. Brandon:

          I’ve never tried this with an AggregateDataSource. My guess is that you’ll also have to provide the ListId, but there’s no clear documentation that I’ve seen for this.

          M.

  4. Worked like a charm. I can’t tahnk you enough for all the material you share! I was able to develop a nice little application linking five lists to create a work package.

  5. hi,

    I have a requirement of opening this attachment in a new window but by following this method documnet opens in a same window.

    Any help is appreciated

  6. Hey Marc!

    I’m trying to use the code snippet you have published, but I keep getting errors. Unfortunately errors that don’t say much.

    “An unexpected error has occurred.”

    I’m using the code snippet as shown below (probably doing it wrong):

    server
    Attachments
    Display
    true

    No

  7. Hi Marc,

    I have added this code to my page and it is now showing the attachments which is great. The only problem is that I had an image on the page and it now doesn’t show and has the message:

    [“Page Image” column value]

    I can delete the code out and the image shows again.

    Any suggestions?

    Thanks.

  8. Shannon:

    I’m not sure what the dependency there would be. The code above is in a DVWP, whereas the image looks to be in a publishing element. I can’t think of a reason why there would be any interaction.

    M.

  9. Hello Mark,
    I am aware of the fact that this is one- almost two years after this post, but i just found it and I have tried it but for me it gives me the same attachment for all my list, even though i have different attachments for each item. o u have any idea why?
    thanks a lot

  10. Hi Mark,

    I have the same issue like Shannon, only that mine puts [‘Name’ column value] in the browser title (code is in a DVWP).

    Any idea?

    Thanks.

    1. I’m facing same problem.

      When my aspx page has 2 or more DVWP that display attachments (by Marc method), just attachments from the first DVWP is displayed.

      The others appears like [‘Attachments’,Column value].

      ps: working in a MOSS 2007 environment and trying to find a solution!

      1. Hi Marc!

        I solved this problem using an amazing jQuery library aka SPServices on DVWP ;p
        I’m using the GetAttachmentCollection method and it’s working very well!!

        Thanks!

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.