Any way to upload a file to sharepoint through webservice

file uploadsharepointsharepoint-2010soap

I have a requirement to upload files to sharepoint using sharepoint webservices.
Are there any sharepoint services which consumes the file content as base64 data inside soap request?

Best Answer

This resource will help you to understand this.

Here's the relevant content from the article in case the link is broken for future readers:

Let’s Upload a Document

To upload a document we need to add another service reference to http://server/sites/personal/_vti_bin/copy.asmx. This is the Copy service.

Here is the code to upload a document to the document library:

 CopySoapClient client = new CopySoapClient();

 if (client.ClientCredentials != null)
 {
     client.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
 }

 try
 {
     client.Open();
     string url = "http://server/sites/personal/My Documents Library/Folder One/Folder Two/";
     string fileName = "test.txt";
     string[] destinationUrl = { url + fileName };
     byte[] content = new byte[] { 1, 2, 3, 4 };

     // Description Information Field
     FieldInformation descInfo = new FieldInformation
                                     {
                                         DisplayName = "Description",
                                         Type = FieldType.Text,
                                         Value = "Test file for upload"
                                     };

     FieldInformation[] fileInfoArray = { descInfo };

     CopyResult[] arrayOfResults;

     uint result = client.CopyIntoItems(fileName, destinationUrl, fileInfoArray, content, out arrayOfResults);
     Trace.WriteLine("Upload Result: " + result);

     // Check for Errors
     foreach (CopyResult copyResult in arrayOfResults)
     {
         string msg = "====================================" +
                      "SharePoint Error:" +
                      "\nUrl: " + copyResult.DestinationUrl +
                      "\nError Code: " + copyResult.ErrorCode +
                      "\nMessage: " + copyResult.ErrorMessage +
                      "====================================";

         Trace.WriteLine(msg);
         _logFactory.ErrorMsg(msg);
     }
 }
 finally
 {
     if (client.State == CommunicationState.Faulted)
     {
         client.Abort();
     }

     if (client.State != CommunicationState.Closed)
     {
         client.Close();
     }
}