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 )
Parameters,
SourceUrl:
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.
Fields:
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.
Stream:
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:\\Documents\Sample.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);
fStream.Close();
Hi,
I have been trying to copy files from Sharepoint server to a different location (local computer)
I have used the above code in this post but copy.getitem method it doesn’t return the byte array nor the fieldinformation array.(i.e byte array and fieldinformation becomes null).
Is this due to permission problem with sharepoint site.(I’ve full access to this site)?
Any clue is highly appreciated.Thanks in advance
By: Suresh on July 1, 2009
at 7:59 pm
The copy web service doesn’t work for downloading list attachments and no out of the box web service does this.The copy web service only works for the documents within a document library. Unfortunately this wasn’t documented anywhere in the MSDN.
Cheers,
Naveen
By: Naveen on December 22, 2009
at 7:21 am
Hi,
I have been trying to copy files from Sharepoint server to a different location (local computer)
I have used the above code in this post but copy.getitem method it doesn’t return the byte array nor the fieldinformation array.(i.e byte array and fieldinformation becomes null).
Is this due to permission problem with sharepoint site.(I’ve full access to this site)?
Any clue is highly appreciated.Thanks in advance
By: Suresh on July 1, 2009
at 7:59 pm
I have a question regarding the following lines:
byte[] fileContents = new Byte[4096];
copyService.GetItem(sourceUrl, out fieldInfoArray, out fileContents);
You define the byte array that stores the bytes returned from the GetItem method, but you have a size of 4096. Won’t this break for larger files? How can we programmatically set this value prior to calling the GetItem method so we know how large to make the byte array?
By: eric on September 3, 2009
at 9:20 pm
Hi Shanthakumar,
This was what i was looking for.. Was really very helpful..
Thanks
By: Ramya Vinayagamoorthy on October 6, 2009
at 3:52 pm
OMG enjoyed reading this post. I added your rss to my reader.
By: Cancitteria on November 25, 2009
at 5:54 am
I’m having the same result as Suresh. Is this the standard behaviour if the file doesn’t exist?
I’m calling this from the ItemAdded method of an ItemEventReceiver, so the item should definitely exist, but I’m wondering if there could be a timing problem or something…
If someone has found an solution to this I’d be most thankful 🙂
By: Nathan on December 1, 2009
at 5:42 am
Yes this code is working fine for share point document library.. but not working for lists attachments 😦
I have files in the List Attachments then I need to copy these files in share point document library. Please of you people help me to solve this issue 🙂
Thanks in advance..
By: Rohit on December 25, 2009
at 4:35 pm
Its probably too late now to tell eric. But for anyone else that got confused.
you dont need to initialize the byte array.
it should be :
byte[] fileContent; // no need to initialize the GetItem takes care of that.
copyService.GetItem(sourceUrl, out fieldInfoArray, out fileContents);
By: Ohbaka on October 2, 2010
at 1:28 am
Please tell me how to get sharepoint shared documents names
in consle application (Client Model)
By: Siddharth Chauhan on December 4, 2010
at 2:49 pm
If there is a Security problem use your own credentials.
for that instead of the given code line
copyService.Credentials = System.Net.CredentialCache.DefaultCredentials;
use this one
Net.NetworkCredential m_credentials = New Net.NetworkCredential(UserId, Pwd, Domain)
copyService.Credentials = m_credentials;
This will solve the Authorization problem
By: Nipesh on March 24, 2011
at 10:23 am
Hi,
How to download the latest versions or all versions of all the documents avaiable in the document library
By: SR on June 1, 2011
at 10:41 am
[…] More: https://ktskumar.wordpress.com/2009/03/08/download-document-from-sharepoint-library-using-webservice/ […]
By: business opportunities on June 25, 2013
at 5:26 am