Partager via


How to show Attachments with DataFormWebPart

If you are using DataFormWebPart and you need to add the list of attachments to the output you have to modify the XSLT to use the AttachmentsField control.

If you simply use  

 <xsl:value-of select="@Attachments"/>

the result displayed will be a boolean (Yes/No) value. If you want to display the list of attachments with corresponding links you must use AttachmentsField control as described below. This control contains the logic needed to get the attachments and render the list with links.

First of all, locate the template with name dvt_1 and add the html code to insert a new column header like in the following example:

     <xsl:template name="dvt_1">
        <xsl:variable name="dvt_StyleName">Table</xsl:variable>
        <xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row"/>
        <table border="0" width="100%" cellpadding="2" cellspacing="0">
            <tr valign="top">
                <xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
                    <th class="ms-vh" width="1%" nowrap="nowrap"></th>
                </xsl:if>
                <th class="ms-vh" nowrap="nowrap">Title</th>
                <th class="ms-vh" nowrap="nowrap">Author</th>
                <th class="ms-vh" nowrap="nowrap">Modified</th>
                 < thclass="ms-vh"nowrap="nowrap">Attachments</th > 
            </tr>
            <xsl:call-template name="dvt_1.body">
                <xsl:with-param name="Rows" select="$Rows"/>
            </xsl:call-template>
        </table>
    </xsl:template>

Now, locate the dvt_1.rowview template and add a new column with the AttachmentsField control:

 <xsl:template name="dvt_1.rowview">
  <tr>
      <xsl:if test="position() mod 2 = 1">
          <xsl:attribute name="class">ms-alternating</xsl:attribute>
      </xsl:if>
      <xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
          <td class="ms-vb" width="1%" nowrap="nowrap">
              <span ddwrt:amkeyfield="ID" 
                        ddwrt:amkeyvalue="ddwrt:EscapeDelims(string(@ID))" 
                        ddwrt:ammode="view">
              </span>
          </td>
      </xsl:if>
      <td class="ms-vb">
          <xsl:value-of select="@Title"/>
      </td>
      <td class="ms-vb">
          <xsl:value-of select="@Editor" disable-output-escaping="yes"/>
      </td>
      <td class="ms-vb">
        <xsl:value-of select="ddwrt:FormatDate(string(@Modified), 1040, 5)"/>
      </td>
      <td class="ms-vb">
          <xsl:element name="SharePoint:AttachmentsField">
          <xsl:attribute name="runat">server</xsl:attribute>
          <xsl:attribute name="ListId">
                  {D2C70ED5-8551-446D-B0CF-AC4B30387CF0}           
</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>
      </td>
  </tr>
</xsl:template>

Important: you must change the value of ListId attribute with your list id!

HTH

Dario

Technorati Tag: Sharepoint Designer,MOSS

Comments

  • Anonymous
    January 21, 2009
    Let me start by saying that your post makes perfect sense.  I'm pretty proficient in XSLT and the problem I'm having is starting to make me pull my hair out. In my experience, when working with a single item view, it is not necessary to include the ItemID or ListID attributes. I am working on creating a multiple item data view web part that displays any attachments for each item. I have this code wich is relatively equivalent to yours above: <SharePoint:AttachmentsField ControlMode="Display" ItemId="{@ID}" EnableViewState="true" FieldName="Attachments" runat="server"/> The issue I am experiencing is that this code works in a single item view, but in a multiple item view it selects first item's ID as all item's 'ItemID' attribute on the SharePoint:AttachmentsField control.  The result is that if the first item has an attachment, that attachment is displayed for all items.  Likewise if the first item has no attachments, all items display no attachment. It is obvious to me that the SharePoint:AttachmentField control is not receiving the correct ID for each item, but rather all are receiving the first item's ID.  I can't tell why that is though.  I have even implemented the code you have above and I get the same result.  Any ideas why?

  • Anonymous
    January 21, 2009
    I managed to figure this one out myself, although I must admit that I do not know why this solves my problem. What I did was add the following code right before my SharePoint:AttachmentField control <SharePoint:AttachmentButton ControlMode="Edit" Enabled="true" ItemId="{@ID}" runat="server" Visible="false"/>

  • Anonymous
    January 22, 2009
    Hi Brandt, in my sample test page I was not able to repro your issue until when I use both yours and mine code. I discovered then that if I insert AttachmentFields twice (no matter if yours or mine code) the second one show your issue. I suppose that the AttachmentsField control use some sort of "context" that in some cases causes your issue. Probably in client or server side code of the control there is a reference to some variable not correctly cleaned. I will investigate the issue this weekend. Dario

  • Anonymous
    February 04, 2009
    Great work. Save us a lot of work. Thanks.

  • Anonymous
    February 17, 2009
    Hi, thanks for your post, is does work (Brandt's solution does not work for me) The funny thing is that, this solution only works when i open the site using the server name on the url (http://servername/), when i use ip or anything else (http://10.100.10.50/), it gives me a server error page. does it happen to you too?

  • Anonymous
    February 23, 2009
    Very nice tip...saved me a lot of time!

  • Anonymous
    March 25, 2009
    Hi, Thanks Dario. Is there way to use this attachment as a hyperlink? I have a TITLE and then the attachments getting shown up. Since the filenames are too long/incorrect naming, I thought of hyperlinking it with Title. Please let me know if you have a feedback. Appreciate,

  • Anonymous
    August 03, 2009
    Hey really Thanks. It worked for me !!!

  • Anonymous
    August 18, 2009
    The comment has been removed

  • Anonymous
    September 02, 2009
    Forgive me since I am relatively new with XSLT, but how should I place your code?  I created an XSLT view of an existing list, but I tried to place your code in a number of places and I'm not getting the desired result.  Can you let me know how you go this to work on your end? Thank you in advance,

  • Anonymous
    September 21, 2009
    For those interested, I found this to be rather helpful: http://www.codeproject.com/KB/sharepoint/DataViewAttachments.aspx Although I would've preferred a full XSLT method, this can be easily modified since it's all javascript based! Thanks Dario for the great tutorial though!

  • Anonymous
    November 22, 2009
    thanks Dario... u help me solve my problem and save a lot of time..

  • Anonymous
    January 05, 2010
    Thanks a lot. Very helpful post!

  • Anonymous
    January 27, 2010
    Great approach! Any suggestions how to display the attachment as an image? thx

  • Anonymous
    February 16, 2010
    Just tried out your solution and bam, worked on the first try.  Thanks!

  • Anonymous
    February 21, 2010
    Hi jasgo, if you want to display attachments in a different way you have to create your own field control and change xslt to use your control instead of SharePoint:AttachmentsField. It's a little bit more complicated but it will works. Check if AttachmentsField is sealed, if not you can inherits from it and try to override the render method. Otherwise you can use reflector to see the code and use it for your control.

  • Anonymous
    April 15, 2010
    I am also having the same issue that Brandt Fuchs had with multiple item data view web part that displays any attachments for each item. I know he mentioned that the solution is to add <SharePoint:AttachmentButton ControlMode="Edit" Enabled="true" ItemId="{@ID}" runat="server" Visible="false"/> right before <SharePoint:AttachmentsField ControlMode="Display" ItemId="{@ID}" EnableViewState="true" FieldName="Attachments" runat="server"/> but I am having hard time combining your xsl format and his. How can I add that as xsl to test if it works or not for me. Any suggestions? Thank you.

  • Anonymous
    July 27, 2010
    Thank you so much for posting this solution. I was struggling with using Lists.asmx to get the URL to the attachments and then I found this post. I had it done 2 minutes later.

  • Anonymous
    October 07, 2010
    I for the most part, got this working.  However, I have the editing option enabled, and when I edit an item, it copies the attachment items from my first item to all items.  If I leave the page, and pull it up again, it's back to normal.  Anyway to stop this from happening?

  • Anonymous
    December 14, 2010
    Thanks for your post. I'm facing same kind of issue when attaching files as my requirement is to upload multiple files using dataview. Please help me to short this out.

  • Anonymous
    December 14, 2010
    Thanks for your post. I'm facing same kind of issue when attaching files as my requirement is to upload multiple files using dataview webpart. Please help me to short this out.

  • Anonymous
    January 11, 2011
    The comment has been removed

  • Anonymous
    April 08, 2011
    Has anyone been able to get this to work when you have to web parts that using the SharePoint:AttachmentsField? I have a Custom List form above that also uses the Attachments field and I can only get one working and not the other.  Everytime I include AttachmentsFields for both the custom List Form and the DVWP, the page does not load or I get an error. Thanks

  • Anonymous
    September 23, 2011
    How do you set it up when you have an aggregate source(mulitple lists) in the dataview?

  • Anonymous
    July 11, 2013
    When designing a DVWP at the root site level (where the list is located), this code works great. If I place the DVWP on a sub-site, however, the attachments column no longer shows anything. The list items show up correctly, just not the attachment filenames. Is there something similar to the ListURL attribute for the attachment field element?

  • Anonymous
    November 14, 2013
    Thank you! That was really helpful! (:

  • Anonymous
    May 18, 2014
    Can we also show last modified date of attachments?