|
The Copy Web Service is a useful Client Side API to allow uploads of files to a Document Library/Folder with their required metadata. As opposed to methods such as WebDAV PUT only one method needs to be called to upload both the data stream of the file and any SharePoint field/column data. The Copy Web Service is located in _vti_bin/copy.asmx in each SharePoint Site/Subsite. Making Visual Studio aware of the Copy Web Service*S Click on the Advanced button in the dialog that appears. Click on the Add Web Reference button. Enter in the location of your Copy Web Service, this is the address of your Site or Subsite + /_vti_bin/Copy.asmx, and cick Go. Visual Studio will render the page returned by SharePoint outlining the methods available for the Copy Web Service. On the right enter in a useful name for the Web Reference and click Add Reference. Your Copy Web Service now accessible from the reference name you provided. Changing the Copy URI from App.config Uploading a file to a Document Library (including a Title) Construct Copy Instance myCopyService.Copy copyInstance = new Test_Console.myCopyService.Copy(); copyInstance.Credentials = System.Net.CredentialCache.DefaultCredentials; The CopyIntoItems require a number of parameters: The easiest part is to define the destination and source URLs of the file. Note that SharePoint displays the source URL as a message when viewing extended properties of items: "This item is a copy of http://null URLS String sourceUrl = "http://null"; String[] destUrl = { "http://localhost/Test Library/newfile.rtf" }; The next part is to define the Field Information. For this we will set the generic Title column, but the CopyIntoItems allows the setting of custom Columns as well. Notice we are also setting the FieldType, this class has most SharePoint Column types such as DateTime, Text and even specific types such as those for Calendars such as AllDayEvent, Field Data myCopyService.FieldInformation[] fieldData = new Test_Console.myCopyService.FieldInformation[1]; fieldData[0] = new myCopyService.FieldInformation { DisplayName = "Title", InternalName = "Title", Type = Test_Console.myCopyService.FieldType.Text, Value = "New Document" }; The next part is to read in the file, the easiest and hackiest method is to just ReadAllBytes using File.ReadAllBytes(String filename). Provide Data Stream byte[] fileStream = System.IO.File.ReadAllBytes(@"C:\temp\test.csv"); And finally reserve a place for the returned information from the Copy, using a null array of CopyResult. Copy Result Storage myCopyService.CopyResult[] copyData; Now call the method (you may want to incase this in a try-catch if you're paranoid) Make the call copyInstance.CopyIntoItems(sourceUrl, destUrl, fieldData, fileStream, out copyData); Your file should now be available in the Document Library. Note the http://null Full Code Full Code myCopyService.Copy copyInstance = new Test_Console.myCopyService.Copy(); copyInstance.Credentials = System.Net.CredentialCache.DefaultCredentials; String sourceUrl = "http://null"; String[] destUrl = { "http://localhost/Test Library/newfile.rtf" }; myCopyService.FieldInformation[] fieldData = new Test_Console.myCopyService.FieldInformation[1]; fieldData[0] = new myCopyService.FieldInformation { DisplayName = "Title", InternalName = "Title", Type = Test_Console.myCopyService.FieldType.Text, Value = "New Document" }; byte[] fileStream = System.IO.File.ReadAllBytes(@"C:\temp\test.csv"); myCopyService.CopyResult[] copyData; copyInstance.CopyIntoItems(sourceUrl, destUrl, fieldData, fileStream, out copyData); Labels |
Copy Web Service

This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. Hosted generously by CustomWare














