Upload a file to SharePoint through the built-in web services

Andy McCluggage picture Andy McCluggage · Aug 28, 2008 · Viewed 64.8k times · Source

What is the best way to upload a file to a Document Library on a SharePoint server through the built-in web services that version WSS 3.0 exposes?

Following the two initial answers...

  • We definitely need to use the Web Service layer as we will be making these calls from remote client applications.

  • The WebDAV method would work for us, but we would prefer to be consistent with the web service integration method.

There is additionally a web service to upload files, painful but works all the time.

Are you referring to the “Copy” service? We have been successful with this service’s CopyIntoItems method. Would this be the recommended way to upload a file to Document Libraries using only the WSS web service API?

I have posted our code as a suggested answer.

Answer

Andy McCluggage picture Andy McCluggage · Aug 29, 2008

Example of using the WSS "Copy" Web service to upload a document to a library...

public static void UploadFile2007(string destinationUrl, byte[] fileData)
{
    // List of desination Urls, Just one in this example.
    string[] destinationUrls = { Uri.EscapeUriString(destinationUrl) };

    // Empty Field Information. This can be populated but not for this example.
    SharePoint2007CopyService.FieldInformation information = new 
        SharePoint2007CopyService.FieldInformation();
    SharePoint2007CopyService.FieldInformation[] info = { information };

    // To receive the result Xml.
    SharePoint2007CopyService.CopyResult[] result;

    // Create the Copy web service instance configured from the web.config file.
    SharePoint2007CopyService.CopySoapClient
    CopyService2007 = new CopySoapClient("CopySoap");
    CopyService2007.ClientCredentials.Windows.ClientCredential = 
        CredentialCache.DefaultNetworkCredentials;
    CopyService2007.ClientCredentials.Windows.AllowedImpersonationLevel = 
        System.Security.Principal.TokenImpersonationLevel.Delegation;

    CopyService2007.CopyIntoItems(destinationUrl, destinationUrls, info, fileData, out result);

    if (result[0].ErrorCode != SharePoint2007CopyService.CopyErrorCode.Success)
    {
        // ...
    }
}