Thursday, May 28, 2009

SharePoint Walktrough - Displaying List Items with their attachments Part 1/2

image The SharePoint Out Of the Box List View Web Part is quite powerful, yet it isn’t able to display the attachments of a List Item. It can show if an item has any attachments (with a nice paper-clip icon) but you won’t be able to download these attachments directly. As with most SharePoint limitation, there is a workaround. Drasko Popovic wrote about a JavaScript based workaround on CodeProject you might want to check out at http://www.codeproject.com/KB/sharepoint/DataViewAttachments.aspx.

In this post I want to presents you another workaround where all processing are done server-side with a single Web Service call (Lists.asmx). You will need SharePoint Designer 2007 (freely downloadable since the 2nd April 2009 at http://www.microsoft.com/downloads/details.aspx?FamilyID=baa3ad86-bfc1-4bd4-9812-d9e710d44f42&displaylang=en ) and of course a MOSS or WSS instance.

As this walkthrough is pretty long for a blog post, it is split into two parts. If you already how to create a Data View web part to display the elements of a list through calls to the Lists.asmx web service, I would suggest you to move to part 2 of this walkthrough.

0 – Principle/Rationale

We have a list in a SharePoint site and want to display the titles of this list’s elements along with direct links to their attachments.

We are going to create a Data View Web Part using SharePoint Designer, this Data View will consume the SharePoint Web Service to get the items of a list along with their attachments. The data view will display these items using a XSL we will define.

As a bonus this method will allows us to display the list items on a different SharePoint site or collection than the list itself.

1 – Create or Open a page

In SharePoint Designer after opening your site, either open an existing page or create a new one. I choose this last option:

image

Make some room for our content in the PlaceHolderMain zone of the page by creating a custom content.

image

Adding a Web Part zone isn’t mandatory but I strongly recommend it at this point. Click in the new Custom Content Zone you created in the design view, then click Insert|SharePoint Controls| Web Part Zone.

image

You should get something looking like that in the Design View:

image

SharePoint Designer is sometimes quite buggy, saving frequently from this point on might be a good idea. You can save the page wherever you want to. If you are creating multiple custom pages, creating a dedicated Document Library to hold these pages is a good idea; such a library already exists if you have activated the publication feature.

2 – Create a Web Service Data Source

imageThe page is now ready to host our Data View web part. Open the Data Source view by clicking Data View| Manage Data Sources.

imageChoose to connect to a web service:

In the Service Description Location enter: http://SP_SITE_URL/_vti_bin/lists.asmx, then click connect and choose the GetListItems operation:

image

imageDouble click on the listName parameter and enter the name or guid (between brackets {}) of your list.



imageValidate the Data Source Properties and check that it is working correctly by trying to see the data returned by this data source:

If a new panel opens with the list data, it means it’s working.

image

Troubleshooting:

If it isn’t working you will be presented with an enigmatic error message giving you absolutely no details:

image

This can be caused by numerous reasons you want to check:

  • The name or guid of the list is invalid
  • You tried to specify other parameters of the Web Service such as QueryOptions or ViewFields. These parameters can’t be specified at this point as the SharePoint Designer Team Blog attests it (http://blogs.msdn.com/sharepointdesigner/archive/2008/06/20/data-source-issues-and-workarounds.aspx). We will modify those latter on.
  • Problem of authentication, I need to investigate this problem but it seems like you sometimes can’t use the integrated authentication even when using a single WFE. In this case you have to change the Web Service Authentication method to basic and to specify a domain account that has the appropriate permission to read the list items and contact the web service.
Changing the Web Service authentication:

Open the Data Source properties

image

Open the login tab and choose basic authentication:

image

Save the details and try again to retrieve the data.

3 – Create the Data View Web Part

We now have a working Data Source, let’s create a web part to display it.

In the Data Source Details, select the ows_LinkTitle and ows_Attachments properties and choose insert this field as a Multiple Item View (Note: you might need to select the Web Part zone in the design view of the page beforehand).

image

You shall now see something like that in the design view.

image

We know have a very basic Data View Web Part but as you may have noticed the ows_attachments fields just display the number of attachments but not the actual links to these attachments.

We will see in part 2 of this walkthrough how to solve this.

22 comments:

Pritesh said...

I tried to connect web service as per instruction but when i try to show data it gives error "The server returned a non-specific error when trying to get data from the data source. Check the format and content of your query and try again. If the problem persists, contact the server administrator."

I tried to reslove as per your link "http://blogs.msdn.com/sharepointdesigner/archive/2008/06/20/data-source-issues-and-workarounds.aspx" but still it giving same error. Any help really appreciated

Joe said...

Aditya,

I must admit that this step can be a pain.

You might try :
- Setting authentication to Basic
- Making sure the account used for retrieval has sufficient permissions

In all cases using SoapUi will help you make sure you don't have any problem on the server side.

Does SoapUi gives you the results expected ?

Joe

liam said...

Getting the same problem....

liam said...

Turns out it was Windows Firewall. make sure to disable that and other firewalls...

--
http://www.ptreviews.net

Joe said...

Thanks liam for the feedback. yet I am a bit surprised, the connection to the data source is supposed to be done on the server side.

Which firewall was causing this problem, the server one or the client one?

Unknown said...

Unable to see any fields called ows_attachments. I am using a testlist with just a title and have uploaded an attachment.
I can see Title and EnableAttachments fields only.

Joe said...

Luis,

Are you using the correct operation of the Web Service. It would seems like you are getting the details of the list, not of the list items.

Make sure to call the operation GetListsItems of the Lists.asmx Web Service (see This Image of SharePoint Designer.

Hope it helps

Liam said...

It was the Windows firewall on the client machine, believe it or not!

Unknown said...

hello
great post.
thanks!

but i have some weird problem. attachments are being displayed pretty well. but when i click to view these attachments, it pops up authentication box. though list has been configured for annonymous view items permission. and then even if you cancel the authentication pop up. it still shows me the document.

can you tell me the reason and solution to avoid it.

Joe said...

Hi Raj,

I would advise you to try pasting the link to the document in a new window/tab and see if you are asked for credentials. If this is the case it might be a problem with the way you site/list acess is configured.

Joe

Raj said...

great post !!!
post is really clean and explanatory
this is what i needed to display tasks with attachments

keep posting ;)

aszia787 said...

Man you are just wow! Awesome! Truly Magician..
One concern I may have is DVWP is not portable, any specific advice when it comes to deploying it?

Joe said...

Hi Aszia,

Thanks for the cheers.

DVWP are indeed not really portable. However in my experience it is possible to move around and duplicate a DVWP with a bit of work. Most often you will need just need to open the pages in SharePoint Designer to change an URL (web service) or list GUID. I found out that exporting the web part to edit it in a text editor is often even quicker.

By the way, to find the GUIDs you need to insert, you might find my bookmarklets useful : http://blog.jonathanroussel.com/2009/09/retrieve-list-and-view-guids-with.html.

Yet, DVWP can hardly be a solution if you are creating a site factory where the same feature must be deployed all over the place. One probably needs to go the hard way and do actual development in such a case.

Spiral said...

Hi Joe,

I have the same problem with Luis (above).

I have succesfully connected with the Web Service, using the "GetListItems" operation, but i cannot find any ows_attachments field. I see the ows_LinkTitle and apart from that i see some fields called ows_FSObjType, ows_FileLeafRef and ows_FileRef

Joe said...

Spiral, I think your problem and Luis's are different, in your case make sure that :
Attachments are enabled

AND THAT

The viewFields parameter references the attachment field OR if it is empty, make sure that the view you reference includes the attachment field OR if both are void, make sure that the default view includes the attachment field.

James said...

Hi Joe,

Could you explain the process steps for..

'The viewFields parameter references the attachment field OR if it is empty, make sure that the view you reference includes the attachment field OR if both are void, make sure that the default view includes the attachment field.'

I am having the same issues as Luis and Spiral.

Joe said...

Hi James,

In Step 2, Did you change the ViewFields parameter value? If you did, you need to make sure that the attachment field is referenced.

If the viewFields parameter is empty, did you change the viewName parameter ? if you did, you need to make sure that the attachment field is referenced.

If both parameters are empty you need to go to your list (using your browser I mean), and make sure that the "Attachments Fields" is displayed in the default view. If it is you shall see a column with a paperclip at the top.
Does is it make sense?

Pascal said...

Joe, Thank you much for this post, it was very helpful for me and your instructions were perfect. It works fine for me. Now, I'd like to filter the attachements, based on part of their name and their extension, to only display the file I need to show up in each column. I am not abe to do so, Can you please put me on the right tracks?
Merci

Joe said...

Hi Pascal,

If I get it right, you want to display a row for each list item. In this row you have multiple column and want to display different kind of attachments in different columns ? In this case it's the XSL you need to tweak. I think duplicating the SplitAttachments in a SplitAttachmentsType1, SplitAttachmentsType2, ... might dot he trick. You would call SplitAttachmentsType1 in the first column and so forth. In SplitAttachmentsType1 you would add another with your condition in the node.

For a SplitAttachmentsTypeDocn it would be something like :
<xsl:if test="string-length($attachmentUrl) != 0">
<xsl:if test="substring-after("$attachmentUrl",".") == 'doc'">
<a href="{$attachmentUrl}"><img style="border:0px" src="/_layouts/images/attach.gif" alt='Open'/></a>
</xsl:if>
</xsl:if>

Is it what you meant ?


Bye,
Jo

Pascal said...

Yes,thank you much Joe, that did the trick! Sorry I did not get back to you earlier, I have been working on other projects...

Jsmith said...

This works great so far. Can additional content types can be pulled and displayed. I am currently pulling from a standard item list but I have added a the Link content type and I would like for the DataForm to pull this URL as well.

New Mexico Service Janice said...

Been having the same problem for awhile now! Arrggh. Maybe it's the firewall at fault? I dunno.