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



Comments (35)

  1. In my previous blog I included the information for downloading files from a document library, using that

  2. milap_great says:

    I am using SharePoint WebServices to upload documents into MOSS site which is on different server. I would like to know how do I upload documents into specific folder inside a document library using webservices.

  3. sowmyancs says:

    while adding your document, give the exact URL of your document libary and the folder. You will get an idea about it from the below post.

    http://blogs.msdn.com/sowmyancs/archive/2008/03/15/create-publishing-pages-in-portal-sites-programmatically.aspx

    Thanks,

    Sowmyan

  4. Shruti Mishra says:

    This is great piece of information. It solved my problem for which i was struggling from 2 days.

    Thanks a ton

  5. sowmyancs says:

    good to hear that 🙂

  6. volothamp says:

    Do you know how to recursivly see all the childs element of a folder in a document library using the webservices?

    I mean, after this:

    ——-

    bjLists.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 it’s a folder (I’ve already found, it has got a "ows_fsobjtype = 1") obtain the child elements?

    Thanks.

  7. RedRider says:

    FYI – For UTF-8 Encoded HTML pages (downloading from Pages library for instance), you may want to write out the Byte Order Mark first:

    fs.Write( new byte[] { 0xEF, 0xBB, 0xBF }, 0, 3 );

  8. Tim G. says:

    Thank you for this blog posting!!!

    How can the metadata from each of the documents being downloaded be accessed using this method?  I would like to save the files into file system folders based on metadata, and also perhaps create an accompaning file containing the rest of the metadata.

  9. Ranvijay says:

    I’m using your code to get bytes from sharepoint document library. In another method i write these bytes to create a file. It works fine except for office 2007 file. For office 2007 file created using above mentioned method, when i try to open, it says "The office open xml file Nyt office 2007 document cannot be opened becuase there are problem with contents.". Detail message is "The file is corrupt and cannot be opened.

  10. ram says:

    Error 2 The type or namespace name ‘ListsService’ does not exist in the namespace ‘SiteDataWebService’ (are you missing an assembly reference?) ConsoleApplication1

    i am getting the above error , i created the console application with your code when i ran it got the error . It is not recognising ‘ListsService’,do i need to create this web service seperatly or add some assembly reference.

  11. sowmyancs says:

    Ram – are you adding the lists.asmx webservice in the web reference ? I think you are adding SiteData.asmx if that is the case then you have to user Lists.asmx webservice.

  12. sowmyancs says:

    Rnjiv – first of all I am sorry for the long delay and I didn’t see any alert on comments in my mailbox.

    office 2007 files are created based upon on the open xml format but though if the byte stream is perfect is should cretate file without any issues.

    For testing it, you can create a docx file in your machine and create .net app to read it and create a new docx based upon that stream and check whether it is opening the file or not.

    Thanks,

    Sowmyan

  13. ram says:

    Thanks! for your reply, i have to download .zip file from sharepoint to my local drive(c:), i just used your code to create a web service which i can use in SSIS packege to download my file. I am not aware of List.asmx & sitedata.asmx,i am new to sharepoint . Please help.

  14. sowmyancs says:

    You can use the above code to download the file from sharepoint.

    Please refer the following URL for getting more information about those webservices.

    http://msdn.microsoft.com/en-us/library/cc752745.aspx

  15. ram says:

    Thanks for your quick reply .

    i created console application with your code.& added web reference as lists.asmx

    but still getting Error The type or namespace name ‘ListsService’ could not be found (are you missing a using directive or an assembly reference?

    please advise.

    Thanks

  16. nagendra says:

    Hi this is very very useful , thank u very much.

  17. Kuldeep Kadyan says:

    Hi,

    Thank u so much…nowhere found the great blog upon download a document using webservice.

    Code is running like a charm.

    Thanks again. Looking forword to get metadata in xml also. Need to copy on different server.

    Kuldeep Kadyan

  18. Tracy says:

    Hi there sowmyancs,

    I've been trying to achieve functionality similar to what you've got here, but using the ows_Attachments attribute as indicated here:  blogs.msdn.com/…/retrieving-sharepoint-list-items-attachment-urls-using-lists-web-service.aspx.  I noticed you also added the 'IncludeAttachmentUrls' QueryOption to obtain this attribute, but you never made use of it.  I cannot seem to get the attribute to show up within the XML returned from the list service, is this an error you've encountered as well?  Is that perhaps why you utilized the other attributes (ows_EncodedAbsUrl and ows_LinkFilename) instead?

    Thanks for any insight!

    -Tracy

  19. Vando Pereira says:

    For those who are getting 'SiteDataWebService' (are you missing an assembly reference?):

    Please pay attention when you are adding "Service Reference" instead of "Service Web Reference" they are different and thats why it isn't solve your problem…

    I Know that in latest versions of VS when you are making a console application, you don't have "Add Service Web Reference" Option.

    So, just click on "Add Service Reference" and after click on "Advanced". On Advanced menu you will see "Add Web Service reference" button:) Use it and everything will work:)

  20. Vando Pereira says:

    For those who are getting 'SiteDataWebService' (are you missing an assembly reference?):

    Please pay attention when you are adding "Service Reference" instead of "Service Web Reference" they are different and thats why it isn't solve your problem…

    I Know that in latest versions of VS when you are making a console application, you don't have "Add Service Web Reference" Option.

    So, just click on "Add Service Reference" and after click on "Advanced". On Advanced menu you will see "Add Web Service reference" button:) Use it and everything will work:)

  21. B Raj says:

    I am following above steps to download the file from sharepoint document library, I am getting error ListsService.Lists, ListsService.Lists namspace not found. I appraciate your help

  22. carla adams says:

    nice article ,,this is the first time i hear about this

    here is a new site u can find it useful too

    http://www.updocs.info

    enter and have a look ,, u will find it exciting

  23. Varun Verma says:

    I am not getting all the items. Only 14 items that are on first page of view.Please let me know the problem

  24. MB says:

    What is "SiteDataWebService.ListsService" ? It is not declared anywhere…

  25. Aniyah says:

    I get a The request failed with HTTP status 403: Forbidden. error when i use this piece of code. can anyone please help.

  26. SR2014 says:

    I am using your code for the below requirement.

    Requirement: Download all the Images from Sharepoint Picture Library using Web Service.

    Environment: SharePoint 2013.

    Problem: How to handle Large Lists where List Throttling is enabled . I am getting below exception.

    I don’t have access to Central Admin, I need to achieve it using Web Service only.

    Message:

    Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.

    InnerText:

    The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.0x80070024

    InnerXML:

    <errorstring xmlns="schemas.microsoft.com/…/">The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.</errorstring><errorcode xmlns="schemas.microsoft.com/…/errorcode>

  27. Varsha says:

    Im kind of new to this field..I get an missing assembly reference for

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

    Can you please help me..! 🙁

  28. david.skala@gm.com says:

    Can you use web service to remotely access a SharePoint List that is housed on a SharePoint Sandbox  Site from a  remote console application

  29. Download multiple file at a time says:

    Is it possible to download all files within a doc library at a time ? or will it be one by one?

  30. Download files at a time says:

    Is it possible to download all files within a doc library at a time ? or will it be one by one?

  31. bhanu says:

    Thank u sooo much it saved my time…Excellent post

  32. SharePoint 2013 Online Training says:

    I absolutely love your blog and find nearly all of your post’s to be precisely what I’m looking for.

    <a href="http://staygreenacademy.com">SharePoint 2013 Online Training</a>