Download Document from SharePoint Library using WebService

In this post, I am going to explain about downloading a document from sharepoint library using webservice, For that we need following things,

  • Read the contents from Document, located in SharePoint Library
  • Create and store the contents on a new document in Local Machine

To get the contents from Document under Sharepoint library,we shall use the GetItem method of Copy WebService. This method will generate a Byte array of the document, and then pass it as a parameter on FileStream Contrutor to create a new document.

The Syntax for the GetItem method as follows,

public uint GetItem ( string SourceUrl, out FieldInformation[] Fields, out byte[] Stream )


A String that contains the absolute source (on the server to which the SOAP request is sent) of the document that is to be retrieved.

An array of FieldInformation objects, passed as an out parameter, that represent the fields and the corresponding field values that can be copied from the retrieved document.

An array of Bytes, passed as an out parameter, that is a base-64 representation of the retrieved document’s binary data.

Return Value:
A UInt32 that returns 0 to indicate that the operation has completed. (There are also two out parameters containing an array of CopyResult objects and an array of FieldInformation objects.)

Below i’m providing a code for downloading a document from SharePoint Library,

//Copy WebService Settings
string webUrl = “http://localhost:1000?;
WSCopy.Copy copyService = new WSCopy.Copy();
copyService.Url = webUrl+”/_vti_bin/copy.asmx”;
copyService.Credentials = System.Net.CredentialCache.DefaultCredentials;

//Source and Destination Document URLs
string sourceUrl = “http://localhost:1000/Shared Documents/Sample.doc”;
string destinationUrl = “C:\DocumentsSample.doc”;

//Variables for Reading metadata’s of a document
WSCopy.FieldInformation fieldInfo = new WSCopy.FieldInformation();
WSCopy.FieldInformation[] fieldInfoArray = { fieldInfo };
WSCopy.CopyResult cResult1 = new WSCopy.CopyResult();
WSCopy.CopyResult cResult2 = new WSCopy.CopyResult();
WSCopy.CopyResult[] cResultArray = { cResult1, cResult2 };

//Receive a Document Contents  into Byte array (filecontents)
byte[] fileContents = new Byte[4096];
copyService.GetItem(sourceUrl, out fieldInfoArray, out fileContents);

//Create a new file and write contents to that document
FileStream fStream = new FileStream(destinationUrl, FileMode.Create, FileAccess.ReadWrite);
fStream.Write(fileContents, 0, fileContents.Length);

3 thoughts on “Download Document from SharePoint Library using WebService

  1. Rohit Reply

    Hi Shantha,
    I need to write a windows service that when invoked, will download all the documents from the sharepoint document library into the local path (physical) on that server.
    Please let me know if that is possible and if there is any challanges I may face. ( like Size of the document that needs to be downloaded)

Leave a Reply

Your email address will not be published. Required fields are marked *