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.