Code Snippet – Download and Upload the SharePoint Documents using the SharePoint Copy Web Service

I have been recently working on the Open XML SDK program where I needed to download the document from the SharePoint document library, update the data using the Open XML SDK, and upload the document back to the SharePoint document library. Eric White from the MSFT has an excellent article if you need to run the Open XML SDK program from the SharePoint Servers which downloads and uploads the document using the SharePoint Object Model.

http://blogs.msdn.com/ericwhite/archive/2010/03/24/modifying-an-open-xml-document-in-a-sharepoint-document-library.aspx

Although my scenario is similar to Eric White’s program as far as Open XML is concerned, I needed to run the Open XML Program on the Non-SharePoint servers where I can’t use the SharePoint Object Model to download and upload the documents on the SharePoint document library. Best way to accomplish this on the Non-SharePoint Servers is using the SharePoint Copy Web Service.

Here are the additional resources on how to upload and download the SharePoint documents from the document library. They have directly or indirectly helped me to come up with the working solution which follows later in this blog.

Following is the working code to download and upload the SharePoint document on the SharePoint document library.

To test the code, create the Console Program and Add the SharePoint Web Service Interface for the SharePoint Copy Web Service URL where document library is hosted. Following program is using the .NET Framework 2.0 based web services framework. Optionally you can use the WCF interface. To add the web service reference in the Visual Studio 2008 and Visual Studio 2010, Click on the Add Service References -> Advanced -> Add Web Reference -> Specify the SharePoint enviornment’s Copy Web Service URL (http://sp2010vm/_vti_bin/copy.asmx).


using ConsoleInterface.sp2010vmCopyService;

namespace ConsoleInterface
{
    class Program
    {
        public static void Main(string[] args)
        {
            // Doc URL
            string docUrl = "<a href="http://sp2010vm/Shared%20Documents/DocTemplate.docx">http://sp2010vm/Shared%20Documents/DocTemplate.docx</a>";

            // Step 1: Download the Document from the SharePoint Libary using the Copy Web Service
            byte[] byteArray = DownloadDoc(docUrl);
            Console.WriteLine("Document Downloaded from the SharePoint Library");

            // Step 2: Manipulate the document with the Open XML
            byteArray = OpenXMLDocProcessor.UpdateDocument(byteArray);
            Console.WriteLine("Document updated by the Open XML SDK");

            // Step 3: Download the Document from the SharePoint Libary using the Copy Web Service
            UploadDoc(docUrl, byteArray);
            Console.WriteLine("Document uploaded to the SharePoint Library");

            Console.WriteLine("Press any key too continue...");
            Console.ReadLine();
        }

        private static byte[] DownloadDoc(string sourceUrl)
        {
            // Instantiate the SP2010VM Copy Service
            ConsoleInterface.sp2010vmCopyService.Copy sp2010VmCopyService = new ConsoleInterface.sp2010vmCopyService.Copy();
            sp2010VmCopyService.Credentials = System.Net.CredentialCache.DefaultCredentials;

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

            // Call the web service to get the desired item
            uint myGetUint = sp2010VmCopyService.GetItem(sourceUrl, out myFieldInfoArray, out myByteArray);

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

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

            return docBinaryData;
        }

        private static void UploadDoc(string sourceUrl, byte[] docBinaryData)
        {
            // Instantiate the SP2010VM Copy Service using Default Windows Credentials
            ConsoleInterface.sp2010vmCopyService.Copy sp2010VmCopyService = new ConsoleInterface.sp2010vmCopyService.Copy();
            sp2010VmCopyService.Credentials = System.Net.CredentialCache.DefaultCredentials;

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

            // To receive the result Xml.
            ConsoleInterface.sp2010vmCopyService.CopyResult[] result;

            // List of desination Urls
            string[] destinationUrls = { Uri.EscapeUriString(sourceUrl) };

            // Upload the document to the SharePoint document library
            sp2010VmCopyService.CopyIntoItems(sourceUrl, destinationUrls, myFieldInfoArray, docBinaryData, out result);
            if (result[0].ErrorCode != ConsoleInterface.sp2010vmCopyService.CopyErrorCode.Success)
            {
                Console.WriteLine("Error occured during document upload process.");
                throw new Exception("Error Occured!");
            }
        }
    }
}

Advertisements
This entry was posted in SP2010 Code Snippets. Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s