Download files from a SharePoint document library remotely via Lists.asmx webservice

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

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

 

Note:

You must create a .cs file with wsdl.exe tool and add it to your project. If you want to add a DLL then you need to use csc.exe to convert your .cs file into a DLL. Both wsdl and csc tools are command-line tools.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值