Posted by: Shantham | March 8, 2009

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 )

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

Read more…

Advertisements

Responses

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

    • 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

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

  3. 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?

  4. Hi Shanthakumar,

    This was what i was looking for.. Was really very helpful..

    Thanks

  5. OMG enjoyed reading this post. I added your rss to my reader.

  6. 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 🙂

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

  8. 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);

  9. Please tell me how to get sharepoint shared documents names
    in consle application (Client Model)

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

  11. Hi,

    How to download the latest versions or all versions of all the documents avaiable in the document library

  12. […] More: https://ktskumar.wordpress.com/2009/03/08/download-document-from-sharepoint-library-using-webservice/ […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: