Friday, May 7, 2010

Download all documents recursively from document library using web service

Hi All,

A SharePoint web service is such a fantastic mean of providing a value to the non SharePoint using applications.

When in any ASP.Net or just any other application needs to use SharePoint library or any other feature, they can use SharePoint web services. And classic example and a common requirement are to download al documents in all folders recursively using web services.

Well, how difficult it is? Answer is that the question is wrong. Ask how easy it is? Well, very easy.

Just take the reference of SharePoint Web Service that you want to work with.

Just for the demo, I have created windows application and used a Lists.asmx web services to pull all documents from all folders within document library.

Here is a simple code for this. On one button click I am copying all files from document library. Just make sure that you change appropriate URL of your site. Also make sure if it is the sub site, then your lists.asmx should point till the sub site path. That means {subsite url} + “/vti_bin/Lists.asmx”;

private void button3_Click(object sender, EventArgs e)
XmlDocument xmldoc = new System.Xml.XmlDocument();

string URL = "";

string FileName = "";

DownloadListItems.Lists objLists = new EncryptAndDecrypt.DownloadListItems.Lists();

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

objLists.Url = "{siteurl}/_vti_bin/lists.asmx";

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

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

XmlNode xmlViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");

XmlNode xmlQueryOptions = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");

xmlQueryOptions.InnerXml = "<ViewAttributes Scope='RecursiveAll' IncludeRootFolder='True' />";

xmlViewFields.InnerXml = "";

xmlQuery.InnerXml = "";


XmlNode xmlListItems = objLists.GetListItems("Shared Documents", null, xmlQuery, xmlViewFields, null, xmlQueryOptions, null); //change the document library name to your document library

XmlNodeList oNodes = xmlListItems.ChildNodes;

foreach (XmlNode node in oNodes)
XmlNodeReader Reader = new XmlNodeReader(node);

while (Reader.Read())
if (Reader["ows_EncodedAbsUrl"] != null && Reader["ows_LinkFilename"] != null)
URL = Reader["ows_EncodedAbsUrl"].ToString();

FileName = Reader["ows_LinkFilename"].ToString();

CopyAndSaveAttachment(URL, FileName);




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


public static void CopyAndSaveAttachment(string URL, string FileName)

HttpWebRequest request;

HttpWebResponse response = null;


request = (HttpWebRequest)WebRequest.Create(URL);

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\" + FileName, FileMode.Create);

byte[] read = new byte[4096];

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





catch (Exception ex)
throw ex;



That is it. You are done with downloading all documents from document library recursively.


Mohammad Rafiq said...

your code saved me. Thank you very much

Anonymous said...

man you are greate! you saved me from critical problem. i will be fkd up if i didn't find your post. thants a lot

Anonymous said...

Short and simple code, One issue i faced so i will note that:
Instead of DownloadListItems.Lists objLists = new EncryptAndDecrypt.DownloadListItems.Lists();


Lists objLists = new Lists();

Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to