Upload a File into a SharePoint Library using Copy Web Service

You probably seen something similar to what I’m about to show you all over the internet. I will be using the Copy Web Service. This service is used to add or copy files from disparate SharePoint sites to other sites or can be used to simply upload files to your SharePoint libraries.

Information about this service can be found here http://msdn.microsoft.com/en-us/library/copy(office.12).aspx

Here is a simple code snippet that will upload a file you’ve selected from the Open File Dialog we all use everyday. I’ve written this inside of a WPF application. You will need to reference the Copy Service and erase or comment some lines in order for you to use it in your application.

   1: private void btnBrowse_Click(object sender, RoutedEventArgs e)

   2:         {

   3:             try

   4:             {

   5:                 _copyProxy = new SharePoint3CopyService.Copy();

   6:                 _copyProxy.Credentials = credentials;

   7:                 _copyProxy.Url = copyWebServiceRelativeUrl;

   8:                 _copyProxy.CopyIntoItemsCompleted += new SharePoint3CopyService.CopyIntoItemsCompletedEventHandler(copyProxy_CopyIntoItemsCompleted);           

   9:  

  10:                 OpenFileDialog openFileDialog = new OpenFileDialog();

  11:                 openFileDialog.RestoreDirectory = true;

  12:  

  13:                 DialogResult dialogResult = openFileDialog.ShowDialog();

  14:  

  15:                 FileInfo fileInfo = new FileInfo(openFileDialog.FileName);

  16:  

  17:                 byte[] fileBytes;

  18:  

  19:                 if (fileInfo.Exists)

  20:                 {

  21:                     Stream fileStream = null;

  22:  

  23:                     string[] destinationUrls = { Uri.EscapeUriString(portalUrl + "/" + listName + "/" + openFileDialog.SafeFileName) };

  24:  

  25:                     using (fileStream = openFileDialog.OpenFile())

  26:                     {

  27:                         fileBytes = new byte[fileStream.Length];

  28:  

  29:                         int numBytesToRead = (int)fileStream.Length;

  30:                         int numBytesRead = 0;

  31:                         while (numBytesToRead > 0)

  32:                         {

  33:                             // Read may return anything from 0 to numBytesToRead.

  34:                             int n = fileStream.Read(fileBytes, numBytesRead, numBytesToRead);

  35:  

  36:                             // Break when the end of the file is reached.

  37:                             if (n == 0)

  38:                                 break;

  39:  

  40:                             numBytesRead += n;

  41:                             numBytesToRead -= n;

  42:                         }

  43:                     }

  44:  

  45:                     SharePoint3CopyService.FieldInformation fieldInformation = new SharePoint3CopyService.FieldInformation();

  46:                     SharePoint3CopyService.FieldInformation[] information = { fieldInformation };

  47:  

  49:                     _copyProxy.CopyIntoItemsAsync(openFileDialog.FileName, destinationUrls, information, fileBytes);

  50:                 }

  51:             }

  52:             catch (Exception ex)

  53:             {

  54:                 selfErrorMessageBuilder.Append(ex.Message);

  55:                 logger.Error("Method: btnBrowse_Click", ex);

  56:             }

  57:         }

 

The code above does the following

a. Gets a reference to the Copy Service (in Visual Studio Web References)

b. Passes a set of credentials. Here, I use Network Credentials – http://msdn.microsoft.com/en-us/library/system.net.networkcredential.aspx

c. I subscribe to the Completed Event

d. Open a new File Dialog so I can select the file to upload

e. Get a FileInfo reference to the File I’ll be working with

f. Set my destination URL. This includes my SharePoint site + Library + File name. I use the SafeFileName property to get the extension of the file along with its name

g. I go ahead and read the stream of bytes till I reach the end. I do this read using the FileStream.Read operation. This will allow me to Write the bytes I ‘m reading into a buffer. More info on this can be found here. http://msdn.microsoft.com/en-us/library/system.io.filestream.read.aspx

h. The FieldInformation class array is simply empty here. I don’t do much with it. I usually use this if I want to keep my metadata intact on the file

Till next time, take it easy!

Advertisements

About jharbieh

I'm an IW Solutions Architect with background in requirements gathering, planning, design, architecture, and development (not necessarily in the right order). Currently, my focus is on the Microsoft Productivity and Collaboration space. Hope you enjoy what I write about here. Thanks for visiting. Johnny Harbieh
This entry was posted in SharePoint Development. Bookmark the permalink.