Navigate Up
Sign In
Supporters of Developer
Web

Copy Web Service

Item is currently unrated. Press SHIFT+ENTER to rate this item.1 star selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+ESCAPE to leave rating submit mode.2 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.3 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.4 stars selected. Press SHIFT+ENTER to submit. Press TAB to increase rating. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.5 stars selected. Press SHIFT+ENTER to submit. Press SHIFT+TAB to decrease rating. Press SHIFT+ESCAPE to leave rating submit mode.
Copy Web Service

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
First we need to have Visual Studio build our class from the Copy web service, to do this first go to your Solution Explorer and right click on your project and select Add Service Reference

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
If you wish to change your Document Library location without having to open Visual Studio, you may edit your application's app.config file to change the Copy location. The Copy location is located in applicationSettings > Project_Name.Properties.Settings > setting name="Test_Console_myCopyService_Copy" > value.

Uploading a file to a Document Library (including a Title)
This method is useful if you wish to upload meta data and a file at the same time to a document library. First construct an instance of your Copy object and pass through your current credentials to authenticate to the SharePoint site.

Construct Copy Instance
myCopyService.Copy copyInstance = new Test_Console.myCopyService.Copy();
copyInstance.Credentials = System.Net.CredentialCache.DefaultCredentials;

The CopyIntoItems require a number of parameters:
string SourceUrl: The original URL of the item (can be "http://null")
string[] DestinationUrls: An array of URLS to save the file into (Full Site address + Document Library + Folders + Filename). This can be of index 1
Project_Name.myCopyInstance.FieldInformation[] Fields: An array of FieldInformation objects that stores the Column's DisplayName, ID, the value of column, Data type
byte[] Stream: The raw byte data of the incoming file
out Project_Name.myCopyInstance.CopyResult[]: The Object that stores result information from the method process.

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 ( Go To Source Item | Unlink )".So you may wish to choose a prettier URL.

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 message.

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);
Categories:
No categories were selected

Comments

Notify me of comments to this article

E-mail:
   

Add Comment

Title:

 
Comment:
Email:

   


Name:

 
Url: