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>
<
th class="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: ,

Comments (27)
  1. Brandt Fuchs says:

    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?

  2. 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"/>

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

  4. Carlos Guaneme says:

    Great work. Save us a lot of work.

    Thanks.

  5. Maclau says:

    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?

  6. Bob Moore says:

    Very nice tip…saved me a lot of time!

  7. P says:

    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,

  8. santhosh says:

    Hey really Thanks. It worked for me !!!

  9. Jason says:

    P, did you find a solution?  I have the same issue…I’d like to use a simple icon that links to the file name.  Our file names are crazy long.

  10. Anthony says:

    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,

  11. TeckniX says:

    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!

  12. oman says:

    thanks Dario…

    u help me solve my problem

    and save a lot of time..

  13. Jeangabi says:

    Thanks a lot. Very helpful post!

  14. jasgo says:

    Great approach! Any suggestions how to display the attachment as an image? thx

  15. Matt B. says:

    Just tried out your solution and bam, worked on the first try.  Thanks!

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

  17. Jason says:

    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.

  18. Steve says:

    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.

  19. Cheyenne says:

    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?

  20. Raja says:

    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.

  21. Raja says:

    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.

  22. Vinayak says:

    Nice post. But, i have a little bit different requirement that i wanted to show only attachments from the specific list onto the DataView webpart.

    Like, if i have a list with name "ABC" which has its own attachment folder. A webpartpage should render all the attachments belongs from that list.

    And most importantly "the DataView should also show the Attachment Name, (Attachment) Modified Date, (Attachment) Modify By and most importantly "(attachment) Comments".

    Can this be possible by using sharepoint designer? I cant see any way out of this, can you please help.

    Thanks in advance.

  23. dr.newoga says:

    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

  24. Brandon says:

    How do you set it up when you have an aggregate source(mulitple lists) in the dataview?

  25. Omegacron says:

    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?

  26. Juliana says:

    Thank you! That was really helpful! (:

  27. K Pankaj says:

    Can we also show last modified date of attachments?

Comments are closed.

Skip to main content