How to download files from a SharePoint document library remotely via Lists.asmx webservice (SPS 2003/ MOSS 2007)

Below I am giving a sample c# code for a .Net Console application, to download the files from a SharePoint Document Library remotely via Lists.asmx web service. We can use the same code for both SharePoint V2 and V3. Basically the application is consuming Lists.asmx web service which is available in the /_Vti_Bin/ location of the site and we can use GetListItems() method for returning the information about document library items as XML.

XmlNode ndListItems = objLists.GetListItems("Shared Documents", null, ndQuery, ndViewFields, null, ndQueryOptions, null);

Using XmlNodeReader we can iterate through each nodes of XML tree and can find out the absolute URL and name of each document library items. For seing the whole XML tree we can use one OuterXml Property of the XmlNode (ndListItems.OuterXml), It will show the all nodes and its childs.

objReader ["ows_EncodedAbsUrl"] will give the URL and objReader ["ows_LinkFilename"] will give the name of the document library item. Once if we get the URL we can download that item to our local machine by using HttpWebRequest & HttpWebResponse classes. We will get the response steam by using the method GetResponseStream(), from this stream we can read the content to a byte array and we can write those byte stream to a physical file location using a FileStream Class.

CODE SNIPPET :

using System;

using System.Collections.Generic;

using

System.Text;

using

System.Net;

using

System.IO;

using

System.Xml;

using

System.Xml.XPath;

using

SiteDataWebService;

namespace

SiteDataWebService

{

class Program

{

public static void DownLoadAttachment(string strURL,string strFileName)

{

HttpWebRequest request;

HttpWebResponse response = null;

try

{

request = (

HttpWebRequest)WebRequest.Create(strURL);

request.Credentials = System.Net.

CredentialCache.DefaultCredentials;

request.Timeout = 10000;

request.AllowWriteStreamBuffering =

false;

response = (

HttpWebResponse)request.GetResponse();

Stream s = response.GetResponseStream();

//Write to disk

FileStream fs = new FileStream(@"C:\DownLoads\"+strFileName, FileMode.Create);

byte[] read = new byte[256];

int count = s.Read(read, 0, read.Length);

while (count > 0)

{

fs.Write(read, 0, count);

count = s.Read(read, 0, read.Length);

}

//Close everything

fs.Close();

s.Close();

response.Close();

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);}

}

static void Main(string[] args)

{

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

XmlNode resnode = null;

string strURL = "";

string strFileName = "";

try

{

ListsService.

Lists objLists = new SiteDataWebService.ListsService.Lists();

objLists.Credentials = System.Net.

CredentialCache.DefaultCredentials;

objLists.Url =

"http://[SITENAME]:34028/sites/TestSite/_vti_bin/lists.asmx"; // change the URL to your sharepoint site

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 = objLists.GetListItems("Shared Documents", null, ndQuery, ndViewFields, null, ndQueryOptions, null); // you can change the document library name to your custom document library name

XmlNodeList oNodes = ndListItems.ChildNodes;

foreach (XmlNode node in oNodes)

{

XmlNodeReader objReader = new XmlNodeReader(node);

while(objReader.Read())

{

if (objReader["ows_EncodedAbsUrl"] != null && objReader["ows_LinkFilename"]!=null)

{

strURL = objReader[

"ows_EncodedAbsUrl"].ToString();

strFileName = objReader[

"ows_LinkFilename"].ToString();

DownLoadAttachment(strURL,strFileName);

}

}

}

Console.ReadLine();

}

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

{

Console.WriteLine("Message:\n" + ex.Message + "\nDetail:\n" + ex.Detail.InnerText + "\nStackTrace:\n" + ex.StackTrace);

Console.ReadLine();

}

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

}

}

}

}


EXCEPTIONS : Below I am listing some of exceptions may occur if we forgot to do something

Exception 1:

Message : {"Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown."}

Cause : This exception may occur if the document library name is incorrect

Exception 2:

Message : {"The request failed with HTTP status 404: Not Found."}

Cause : This exception may occur if the Site URL is incorrect

Exception 3:


Message : {"Access denied to the path..."}

Cause : Access permissions to the physical folder for writing the files