views:

2225

answers:

4

I am attempting to load document files into a document library in SharePoint using the CopyIntoItems method of the SharePoint Copy web service.

The code below executes and returns 0 (success). Also, the CopyResult[] array returns 1 value with a "Success" result. However, I cannot find the document anywhere in the library.

I have two questions:

  1. Can anyone see anything wrong with my code or suggest changes?
  2. Can anyone suggest how I could debug this on the server side. I don't have a tremendous amount of experience with SharePoint. If I can track what is going on through logging or some other method on the server side it may help me figure out what is going on.

Code Sample:

string[] destinationUrls = { Uri.EscapeDataString("https://someaddress.com/Reports/Temp") };

SPCopyWebService.FieldInformation i1 = new SPCopyWebService.FieldInformation { DisplayName = "Name", InternalName = "Name", Type = SPListTransferSpike1.SPCopyWebService.FieldType.Text, Value = "Test1Name" };
SPCopyWebService.FieldInformation i2 = new SPCopyWebService.FieldInformation { DisplayName = "Title", InternalName = "Title", Type = SPListTransferSpike1.SPCopyWebService.FieldType.Text, Value = "Test1Title" };

SPCopyWebService.FieldInformation[] info = { i1, i2 };

SPCopyWebService.CopyResult[] result;

byte[] data = File.ReadAllBytes("C:\\SomePath\\Test1Data.txt");

uint ret = SPCopyNew.CopyIntoItems("", destinationUrls, info, data, out result);

Edit that got things working:

I got my code working by adding "http://null" to the SourceUrl field. Nat's answer below would probably work for that reason. Here is the line I changed to get it working.

// Change
uint ret = SPCopyNew.CopyIntoItems("http://null", destinationUrls, info, data, out result);
+1  A: 

I didn't understand very well what you're tying to do, but if you're trying to upload a file from a local directory into a sharepoint library, i would suggest you create a webclient and use uploadata:

Example (VB.NET):

dim webclient as Webclient 
webClient.UploadData("http://srvasddress/library/filenameexample.doc", "PUT", filebytes)

Then you just have to check in the file using the lists web service, something like:

listService.CheckInFile("http://srvasddress/library/filenameexample.doc", "description", "1")

Hope it was of some help.

EDIT: Don't forget to set credentials for the web client, etc.

EDIT 2: Update metada fields using this:

listService.UpdateListItems("Name of the Library, batchquery)

You can find info on building batch query's in here: link

v3ga
+1 - That is a good approach. However, I want to set the metadata associated with the file at the same time.
Matt Spradley
The Metada data can be updated by the code i've just added up.
v3ga
+2  A: 

I think the issue may be in trying to set the "Name" property using the webservice. I have had some fail doing that. Given the "Name" is the name of the document, you may have some success with

    string targetDocName = "Test1Name.txt";
    string destinationUrl = Uri.EscapeDataString("https://someaddress.com/Reports/Temp/" + targetDocName);
    string[] destinationUrls = { destinationUrl };

    SPCopyWebService.FieldInformation i1 = new SPCopyWebService.FieldInformation { DisplayName = "Title", InternalName = "Title", Type = SPListTransferSpike1.SPCopyWebService.FieldType.Text, Value = "Test1Title" };
    SPCopyWebService.FieldInformation[] info = { i1};
    SPCopyWebService.CopyResult[] result;
    byte[] data = File.ReadAllBytes("C:\\SomePath\\Test1Data.txt");
    uint ret = SPCopyNew.CopyIntoItems(destinationUrl, destinationUrls, info, data, out result);

Note: I have used the "target" as the "source" property. Don't quite know why, but it does the trick.

Nat
Putting in a source Url did the trick. I don't know why no error information is returned when the source Url is not included.
Matt Spradley
A: 

isnt it fun how sourceurl is irrelevant but will only accept some garbage values and not others? that's classic sharepoint. not having a checkin function in the lists service to call after getting copy to work is just bonus sharepoint...

A: 

The sourceurl is used in Sharepoint. It is a link back to the "Source Document." When in your document library, hover over the item, to the right appears a down pointing triangle. Clicking on it, brings up a menu. Click on the "View Properties" Option. On this page you will see the following "This item is a copy of http://null ( Go To Source Item | Unlink )"

Because we are using the Copy function Sharepoint is keeping track of the "Source item" as part of the Document Management feature.

Lewis