Friday, February 20, 2009

Download a File Using the SharePoint Copy Web Service

Here is a code sample I put together to download a file from a Document Library using SharePoint's Copy web services.
// sharepoint is a web reference that points to http://sharepoint/_vti_bin/copy
sharepoint.Copy myCopyService = new sharepoint.Copy();
myCopyService.Credentials = System.Net.CredentialCache.DefaultCredentials;

// URL to the file you want to downlaod
string copySource = "https://sharepoint/file.doc";

// Define the variables that will store the output from the web service call
sharepoint.FieldInformation myFieldInfo = new sharepoint.FieldInformation();
sharepoint.FieldInformation[] myFieldInfoArray = { myFieldInfo };
byte[] myByteArray;

// Call the web service
uint myGetUint = myCopyService.GetItem(copySource, out myFieldInfoArray, out myByteArray);

// Convert into Base64 String
string base64String;
base64String = Convert.ToBase64String(myByteArray,0,myByteArray.Length);

// Convert to binary array
byte[] binaryData = Convert.FromBase64String(base64String);

// Create a temporary file to write the text of the form to
string tempFileName = Path.GetTempPath() + "\\" + myFieldInfoArray[0].Value;

// Write the file to temp folder
FileStream fs = new FileStream(tempFileName, FileMode.CreateNew, FileAccess.ReadWrite);
fs.Write(binaryData, 0, binaryData.Length);

//Open file in default program

There is more information on the Copy web service here: Look under the Copy class and its GetItem method.


  1. Awesome article. I implemented successfully.

    Is there any option that It will show open or save as dialog box?

    1. Disha, do you have code that will open or save file using copy webservice?

  2. Is there any way to download all versions of a file?

  3. I have to ask, why are you converting to base64 and then back from base64?
    I get that it might be nice to demonstrate this for noobs, but as far as I can tell it's a pointless procedure that most people who download this snippet will continue to use time and time again. (As a consultant, I have seen this article cited in 3 different development houses now)

    What might have been better to explore is the process for obtaining the filename from the metadata returned, instead of parsing the name from the URL (which would be equally as valid as iterating FieldInfo, find the field with FileLeafRef as the internal name).

    From your example one might assume that the filename is always the first element in the field array, where in fact I find that content type id is often first.

    So a great post, that can easily mislead. Would have had a far greater contribution if the code comments had included justification for the use of the commented code, rather than being a simple english notation of the code.

    1. lol, Couldn't you live without typing all this? people were loving the post..:D

    2. Chris ima with u..really ive been following this blindly until need to encode then decode back of base64. dunno guys why is that happend?pls somebudy clarify

    3. This comment has been removed by the author.

  4. the myFieldInfo is always null

  5. Hey there! I am glad to stop by your site and know more aboutcopying services. Keep it up! This is a good read. I will be looking forward to visit your page again and for your other posts as well. Thank you for sharing your thoughts about copying services in your area.
    The Volume Snapshot Service was first added to Microsoft Windows in Windows XP; this version of VSS is used by NTBackup, however it can only create non-persistent snapshots (a temporary snapshot, usually used for creating a file-based backup or more generally, accessing copies of files that have been locked by applications for editing). NTBackup uses a proprietary BKF file format to store the shadow copies permanently.
    Quality is how we've grown our business – and our customers' businesses as well. And it's how we can grow your business, too.

    Salem MA

  6. GSCopy Pro v6.0 (RoboCopy Alternative) with Open File Agent
    GSCopyPro is a single command-line tool (CLI) that can copy, replicate and move files from one folder to another. This folder can be on the same machine/ server or another server elsewhere. What makes GSCopyPro stand out from other competitors is the fact it works on 32-bit as well as 64-bit systems and has no restrictions. It can easily be scheduled to run as a scheduled task and fully automated. GSCopyPro also comes with an open file agent which can copy files that are locked/ opened by other processes. This feature is supported in all windows versions from widows XP/ 2003 and later.
    Go To:>>