SharePoint 2010: Adding List Attachments as Links in a DataView Webpart

As silly as it seems to me, providing links to list attachments in a data view is still not an OOB option in SharePoint 2010. I read a lot about how you should just use document libraries instead, and while that may be a better option in some ways, it doesn't always provide the best user experience, especially when you want to provide a non technical user a way to have files associated with a custom list item, or in my case, as Task List item. The only OOB way to link a list to a document library such that you could later provide a filtered view of the documents was to link up some sort of common data point (such as a project number, or a list item ID number). While that's not a big deal usually, the goal to achieve as little effort for the end user is always paramount in my firm. Feel free to comment on how best to automatically link an item(s) to a document library without custom code or a third party solution.

For me, the easiest thing for my users, is to simply attach to the list. No worries for them... No trouble shooting for me when things don't show up in a view somewhere. I have seen a lot of posts on how to add this link to a view or a data view in SharePoint Designer 2010, and it honestly took me a while to figure out where the markup I found actually needed to go. I will attempt to show you how I did this as clear as I can for someone who is new to SharePoint 2010 Designer as I am.

The first thing I do is create a new web part page, doesn't matter where, just some where inside the site where your list lives. This page will just be testing/build page for our dataview.

Once created, edit the new page (Advanced mode is optional).

Important: This is the silly part, but if you aren't aware of the new interface subtleties, it can haunt you for hours, as it did me. :P
Click in a web part zone in the editor, then on the ribbon, the "INSERT" tab should be available, Click the "DATA VIEW" and choose "Empty Data View". The reason for this is, over choosing the list directly from this menu, is that the XSL template code is different (and hidden by default) than what you may be used to in 2007.

From here you click on the empty data view to choose a data source (this is where you pick your list) and then the fields from that list you want to display. For this exercise you will at least need the "Attachments" field.


Once your Attachments field is on your data view, we can now go into the XSL in code view and add some simple markup.

As you can see at several posts I've found (Jim Mathew's Blog being one of them) you simply need to add:

<SharePoint:AttachmentsField ControlMode=”Display” ItemId=”{@ID}” EnableViewState=”true” FieldName=”Attachments” runat=”server”/>



As seen below in the XSL test block... in this case, the default content in this block is the word "Yes". I replaced the word "Yes" with the markup:



Save the page, browse to it and your attachment(s) links should display.

I took it a step further and added query string value filtering for use within the system I was building, so that you could see all attachments for all list items for a specific project.

Hope this helps!

Comments

Post a Comment

Popular posts from this blog

Get users of AD group for use in SharePoint webpart.

A simple method for finding Active Directory user information using C# with LDAP