次の方法で共有


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 =

"https://[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

  • Anonymous
    September 25, 2007
    In my previous blog I included the information for downloading files from a document library, using that

  • Anonymous
    June 17, 2008
    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.

  • Anonymous
    June 17, 2008
    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

  • Anonymous
    September 18, 2008
    This is great piece of information. It solved my problem for which i was struggling from 2 days. Thanks a ton

  • Anonymous
    September 19, 2008
    good to hear that :)

  • Anonymous
    October 05, 2008
    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.

  • Anonymous
    November 13, 2008
    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

  • Anonymous
    November 19, 2008
    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 );

  • Anonymous
    November 20, 2008
    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.

  • Anonymous
    November 26, 2008
    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.

  • Anonymous
    February 24, 2009
    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.

  • Anonymous
    February 26, 2009
    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.

  • Anonymous
    February 26, 2009
    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

  • Anonymous
    February 27, 2009
    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.

  • Anonymous
    February 27, 2009
    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

  • Anonymous
    February 27, 2009
    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

  • Anonymous
    July 02, 2009
    Hi this is very very useful , thank u very much.

  • Anonymous
    October 28, 2009
    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

  • Anonymous
    June 15, 2010
    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

  • Anonymous
    June 28, 2010
    Here is my solution: mysplist.blogspot.com/.../sharepoint-2007-web-service-download.html

  • Anonymous
    June 28, 2010
    Here is my solution: mysplist.blogspot.com/.../sharepoint-2007-web-service-download.html

  • Anonymous
    January 24, 2011
    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:)

  • Anonymous
    January 24, 2011
    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:)

  • Anonymous
    February 14, 2011
    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

  • Anonymous
    April 13, 2011
    nice article ,,this is the first time i hear about this here is a new site u can find it useful too www.updocs.info enter and have a look ,, u will find it exciting

  • Anonymous
    September 18, 2013
    I am not getting all the items. Only 14 items that are on first page of view.Please let me know the problem

  • Anonymous
    October 16, 2013
    What is "SiteDataWebService.ListsService" ? It is not declared anywhere...

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

  • Anonymous
    March 03, 2014
    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>

  • Anonymous
    March 24, 2014
    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..! :(

  • Anonymous
    April 21, 2014
    Can you use web service to remotely access a SharePoint List that is housed on a SharePoint Sandbox  Site from a  remote console application

  • Anonymous
    July 31, 2014
    Is it possible to download all files within a doc library at a time ? or will it be one by one?

  • Anonymous
    July 31, 2014
    Is it possible to download all files within a doc library at a time ? or will it be one by one?

  • Anonymous
    January 22, 2015
    Thank u sooo much it saved my time...Excellent post

  • Anonymous
    March 26, 2015
    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>