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

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

Rate This
  • Comments 33

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
  • In my previous blog I included the information for downloading files from a document library, using that

  • 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.

  • 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

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

    Thanks a ton

  • good to hear that :)

  • 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.

  • Hey volothamp,

    see the following url for getting a recursive list of files in sharepoint.

    http://sqlblogcasts.com/blogs/drjohn/archive/2007/11/02/Getting-a-list-of-files-from-a-moss-document-library-using-a-SharePoint-web-service.aspx

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

  • 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.

  • 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.

  • 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.

  • 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.

  • 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

  • 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.

  • 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

Page 1 of 3 (33 items) 123
Leave a Comment
  • Please add 3 and 5 and type the answer here:
  • Post