Showing posts with label Web Services. Show all posts
Showing posts with label Web Services. Show all posts

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);
fs.Close();

//Open file in default program
System.Diagnostics.Process.Start(tempFileName);

There is more information on the Copy web service here: http://msdn.microsoft.com/en-us/library/copy.aspx. Look under the Copy class and its GetItem method.