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.
Now that I know the trick, some Binging shows me that lots of others have known all along. See, keep learning!
Hi Marc,
I’m trying to access a list on a root site from a subsite and I did that by creating a web part page and inserting DVWP on the subsite of the root list. I have the attachment problem though where the list item shows either Yes or No whether the item has an attachment. When I replace the code with what’s shown on this page i get this error when refreshing the page.
“An unexpected error has occurred.
Web Parts Maintenance Page: If you have permission, you can use this page to temporarily close Web Parts or remove personal settings. For more information, contact your site administrator. ”
I’ve tried everything that’s on the web, but I can never get a link of the attachments to show successfully from the subsite. Do you know how I can fix this problem?
Thanks
Forgot to mention that I’m doing this on a WSS 3.0 collaboration site using Designer 2007
David:
Consider using client-side script and SPServices to grab the attachment info. It can often be cleaner and it’s not that “expensive” if you aren’t showing a huge number of items in the view.
M.
Hi Marc, wonderful post, thanks! Is there a way to sort the attachments? Best, s’
Guess not huh
I’d probably do this with script these days. Once you have the list of attachments, you can sort them in any way you’d like before you put them into the page.
M.
s’
Honestly, I’m not sure. Would it add that much value, though? If so, it probably would be easier to sort with script than in the DVWP.
M.
You saved my day! Thanks!
Any Ideas on how to achieve this in SharePoint 2013? Please help!!
Ram:
I haven’t done this with 20103, but I don’t think there’s any reason it wouldn’t work there as well.
M.
Thank you so much! Its cool…..actually I had come across a similar solution but it was using list id. Though I enetred correct list id still it was giving me an error. Your solution worked without any issue.
not able to delete the attachment
I got this to work but how can we open these attachments in new window ?
nadeem:
I don’t think we have any options to do that in the XSL. I’d suggest adding a little JavaScript to change the target of the link on page load.
M.
Hey thanks for that! I just wanted to leave one missing bit to get this to work: Don’t forget to import the `SharePoint` namespace:
`xmlns:SharePoint=”Microsoft.SharePoint.WebControls”`