Hi Guys,

In this article we will review how to upload files to SharePoint using the default SharePoint Web service http://site/_vti_bin/copy.asmx.
For this example, we will use a C# console application, to accomplish this task you need to :

1- Create a new project C# Console application

2- Add a reference to your http://site/_vti_bin/copy.asmx

3- Use the following code

PS:In the next post, I will detail how to assign metadata to the files, particularly useful, in case you use a custom content type.

static void Main(string[] args)
        {

            //Copy WebService Settings
            string webUrl = "https://YouSiteHere";
            WSCopy.Copy copyService = new WSCopy.Copy();
            copyService.Url = webUrl + "/_vti_bin/copy.asmx";
            copyService.Credentials = System.Net.CredentialCache.DefaultCredentials;

            //Declare and initiates the Copy WebService members for uploading
            string sourceUrl = @"C:\MyFile.pdf";
            string[] destinationUrl = { "https://YourSiteHere/DocumentLibrary/MyFile.pdf" };
            WSCopy.CopyResult cResult1 = new WSCopy.CopyResult();
            WSCopy.CopyResult cResult2 = new WSCopy.CopyResult();
            WSCopy.CopyResult[] cResultArray = { cResult1, cResult2 };
            WSCopy.FieldInformation fFiledInfo = new WSCopy.FieldInformation();
            //fFiledInfo.DisplayName = "Title";
            //fFiledInfo.Type = WSCopy.FieldType.Text;
            //fFiledInfo.Value = "Sample Description";
            WSCopy.FieldInformation[] fFiledInfoArray = { fFiledInfo };

            //Reading the document contents in to stream
            FileStream strm = new FileStream(sourceUrl, FileMode.Open, FileAccess.Read);
            byte[] fileContents = new Byte[strm.Length];
            byte[] r = new Byte[strm.Length];
            int ia = strm.Read(fileContents, 0, Convert.ToInt32(strm.Length));
            strm.Close();

            //Copy the document from SourceUrl to destinationUrl with metadatas
            uint copyresult = copyService.CopyIntoItems(sourceUrl, destinationUrl, fFiledInfoArray, fileContents, out cResultArray);

            if (copyresult == 0)
                Console.WriteLine("Document uploaded successfully from " + sourceUrl + " to " + destinationUrl[0]);
            else
                Console.WriteLine("Document gets failed on uploading..");

            Console.Write("Press any key to exit...");
            Console.Read();


        }

//Enjoy :)

Originally posted 2015-11-20 10:17:03.

Leave a comment