Welcome to MSDN Blogs Sign in | Join | Help

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

Published Saturday, September 15, 2007 12:09 AM by sowmyancs

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

# How to enumerate the items inside a SharePoint Document Library folder

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

Tuesday, September 25, 2007 11:52 AM by Sharing my ideas,thoughts & findings in SharePoint Technologies

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

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.

Tuesday, June 17, 2008 10:40 PM by milap_great

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

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

Tuesday, June 17, 2008 11:14 PM by sowmyancs

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

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

Thanks a ton

Friday, September 19, 2008 6:16 AM by Shruti Mishra

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

good to hear that :)

Friday, September 19, 2008 12:20 PM by sowmyancs

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

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.

Monday, October 06, 2008 6:41 AM by volothamp

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

Thursday, November 13, 2008 2:11 PM by dan

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

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 );

Wednesday, November 19, 2008 4:24 PM by RedRider

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

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.

Thursday, November 20, 2008 8:29 AM by Tim G.

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

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.

Thursday, November 27, 2008 6:28 AM by Ranvijay

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

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.

Tuesday, February 24, 2009 9:45 AM by ram

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

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.

Thursday, February 26, 2009 2:41 PM by sowmyancs

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

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

Thursday, February 26, 2009 5:09 PM by sowmyancs

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

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.

Friday, February 27, 2009 12:56 PM by ram

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

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

Friday, February 27, 2009 1:12 PM by sowmyancs

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

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

Friday, February 27, 2009 3:37 PM by ram

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

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

Friday, July 03, 2009 12:50 AM by nagendra

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

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

Thursday, October 29, 2009 4:06 AM by Kuldeep Kadyan

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker