Welcome to MSDN Blogs Sign in | Join | Help

Retrieving SharePoint List Items' Attachment URLs using Lists Web Service

Imagine a remote application that uses SharePoint Web Services to retrieve a list item’s attachments to perform some action on them or provide a link to them. This application could have been coded in WSS 2.0 using a mix of the GetListItems and GetAttachmentCollection methods of the Lists Web Service. However, the performance of such an app is extremely poor, since the developer had to issue a GetAttachmentCollection call for each item on the list.

In WSS 3.0, getting the complete list of attachments of each of the items in a list can be accomplished with a single call to GetListItems, using the IncludeAttachmentUrls query parameter, which is new for 3.0. The WSS 3.0 SDK’s GetListItems reference and how-to currently does not provide any documentation for this new parameter, so I hope this blog entry will sufficiently compensate for that in the interim. The way to use the IncludeAttachmentUrls parameter is shown in the following snippet of code:

Web_Reference_Folder_Name.Lists listService = new Web_Reference_Folder_Name.Lists();

listService.Credentials = System.Net.CredentialCache.DefaultCredentials;

 

XmlDocument xmlDoc = new System.Xml.XmlDocument();

XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");

XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields",

"");

XmlNode ndQueryOptions = xmlDoc.CreateNode(XmlNodeType.Element,

"QueryOptions", "");

 

ndQueryOptions.InnerXml =

"<IncludeAttachmentUrls>TRUE</IncludeAttachmentUrls>";

ndViewFields.InnerXml = "";

ndQuery.InnerXml = "";

 

try

{

XmlNode ndListItems = listService.GetListItems("List_Name", null,

ndQuery, ndViewFields, null, ndQueryOptions);

      MessageBox.Show(ndListItems.OuterXml);

}

 

catch (System.Web.Services.Protocols.SoapException ex)

{

MessageBox.Show("Message:\n" + ex.Message + "\nDetail:\n" +

ex.Detail.InnerText + "\nStackTrace:\n" + ex.StackTrace);

}

The XML fragment returned will contain the List Item Attachments under the ows_Attachments Field, delimited by ;#. An example of a row of data returned by this method is shown below:

<z:row ows_ID="2" ows_ContentTypeId="0x0104001CAAB92900F22E4CBEF571BC76308DBE" ows_ContentType="Announcement" ows_Title="test" ows_Modified="2007-02-25 14:23:31" ows_Created="2007-02-25 14:21:02" ows_Author="1073741823;#System Account" ows_Editor="1073741823;#System Account" ows_owshiddenversion="3" ows_WorkflowVersion="1" ows__UIVersion="512" ows__UIVersionString="1.0" ows_Attachments=";#http://server/Lists/Announcements/Attachments/2/test.txt;#http://server/Lists/Announcements/Attachments/2/UIP_Setup.log;#" ows__ModerationStatus="0" ows_LinkTitleNoMenu="test" ows_LinkTitle="test" ows_SelectTitle="2" ows_Order="200.000000000000" ows_GUID="{17DD533F-97A2-4DB2-B925-A941D8BFFBD6}" ows_FileRef="2;#Lists/Announcements/2_.000" ows_FileDirRef="2;#Lists/Announcements" ows_Last_x0020_Modified="2;#2007-02-25 14:21:02" ows_Created_x0020_Date="2;#2007-02-25 14:21:02" ows_FSObjType="2;#0" ows_PermMask="0x7fffffffffffffff" ows_FileLeafRef="2;#2_.000" ows_UniqueId="2;#{FA303EDB-4009-4CC5-9EF2-E7402D3BD0C1}" ows_ProgId="2;#" ows_ScopeId="2;#{DEDE57E4-DE8E-4CF9-9BBC-924175CED304}" ows__EditMenuTableStart="2_.000" ows__EditMenuTableEnd="2" ows_LinkFilenameNoMenu="2_.000" ows_LinkFilename="2_.000" ows_ServerUrl="/Lists/Announcements/2_.000" ows_EncodedAbsUrl="http://server/Lists/Announcements/2_.000" ows_BaseName="2_" ows_MetaInfo="2;#" ows__Level="1" ows__IsCurrentVersion="1" ows_Body="<div class=ExternalClass0BA3FF0220C34E74A624A1FC0C7F0DA8> <div>test</div></div>" />

SDK References

·         WSS 2.0 Lists Web Service: http://msdn2.microsoft.com/en-us/library/aa152619.aspx.

·         WSS 3.0 Lists Web Service: http://msdn2.microsoft.com/en-us/library/lists.aspx.

 

Luis Augusto Angel Mex
SDE/T for SharePoint

 

Published Sunday, March 25, 2007 8:35 PM by sptblog
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# list &raquo; Retrieving SharePoint List Items&#39; Attachment URLs using Lists Web &#8230;

# re: Retrieving SharePoint List Items' Attachment URLs using Lists Web Service

I don't suppose you could explain how to use this code with WSS 3.0 without the 7th argument in the GetListItems call (webId).  Documentation says its optional, but the signarue requires "somethign" and if you don't put a properly formatted Guid string in it, it SharePoint throws an error concerning a Guid.

TIA

Thursday, April 05, 2007 4:03 PM by B.J. Pohl

# QueryOptions

Is there documentation for query options? Schema?

Thanks,

E.

Thursday, May 03, 2007 1:13 PM by ET

# re: Retrieving SharePoint List Items' Attachment URLs using Lists Web Service

How does one use the list web service to actually add and retrieve the actual docs to a document library?

Thanks

Wednesday, June 27, 2007 11:27 AM by Rocco Mastrangelo

# re: Retrieving SharePoint List Items' Attachment URLs using Lists Web Service

Apparently, the URL field in the Lists web service object is not filled in. I see some config entries in the app.config that show the proper URLs to the web service references I added.

I assume that with the URL field empty, they get the information from the app.config.

The seventh parameter is needed if the URL is not specified. If I explicitly set the URL field, I no longer need the seventh parameter.

Wednesday, April 09, 2008 12:03 PM by jjester

# re: Retrieving SharePoint List Items' Attachment URLs using Lists Web Service

Is it possible to make this work via a datasource in SharePoint Designer.  I can get the web service call to work, but I get an error whenever I try to set the QueryOptions node.  I think this is probably a problem converting string to xmlNode, but I'm hoping somebody can tell me about a workaround.

Monday, April 21, 2008 4:41 PM by Ryan Miller

# re: Retrieving SharePoint List Items' Attachment URLs using Lists Web Service

Is there any programmatic way to remotely retrieve the List templates themselves, from a SharePoint server?

Tuesday, September 23, 2008 7:09 PM by pogrebs

# re: Retrieving SharePoint List Items' Attachment URLs using Lists Web Service

Do you know how to list attachment from a subfolder? Using the same path in GetListItems.

Monday, February 09, 2009 6:26 AM by JoaoPrata

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker