How to copy a file to SharePoint using WebService?

I am writing a winforms C # 2.0 application that needs to put an XML file in a document library in SharePoint.

I want to use WebService instead of using the object model (without a link to sharepoint.dll here)

I am currently using http: //webserver/site/_vti_bin/copy.asmx webservice.

Here is the code:

byte[] xmlByteArray; using (MemoryStream memoryStream = new MemoryStream()) { xmlDocument.Save(memoryStream); xmlBytes = memoryStream.ToArray(); } string[] destinationUrlArray = new string[] {"http://webserver/site/Doclib/UploadedDocument.xml"}; FieldInformation fieldInfo = new FieldInformation(); FieldInformation[] fields = { fieldInfo }; CopyResult[] resultsArray; using (Copy copyService = new Copy()) { copyService.Credentials = CredentialCache.DefaultCredentials; copyService.Url = "http://webserver/site/_vti_bin/copy.asmx"; copyService.Timeout = 600000; uint documentId = copyService.CopyIntoItems("", destinationUrlArray, fields, xmlByteArray, out resultsArray); } 

When this code works, I get the only result in the resultArray parameter:

 DestinationURL: "http://webserver/site/Doclib/UploadedDocument.xml" ErrorCode: UnKnown ErrorMessage: "Object reference not set to an instance of an object." 

From my search I found a couple of possible clues.

  • Microsoft TechNet - Copy.asmx copyintoitems will only work if the source and destination URLs are in the same SPWebApplication (Site Collection).

  • Microsoft Social - "The object reference is not installed in the object instance. An error occurs because SharePoint was unable to determine this specific property."

This leads me to believe that my source url should be configured for something, but what? This occurs from the client workstation and does not have a source URL.

Any help would be appreciated.

hank you
Kate

+7
c # web-services file-upload sharepoint
source share
10 answers

Here is what works at the moment:

 WebRequest request = WebRequest.Create("http://webserver/site/Doclib/UploadedDocument.xml"); request.Credentials = CredentialCache.DefaultCredentials; request.Method = "PUT"; byte[] buffer = new byte[1024]; using (Stream stream = request.GetRequestStream()) { using (MemoryStream memoryStream = new MemoryStream()) { dataFile.MMRXmlData.Save(memoryStream); memoryStream.Seek(0, SeekOrigin.Begin); for (int i = memoryStream.Read(buffer, 0, buffer.Length); i > 0; i = memoryStream.Read(buffer, 0, buffer.Length)) { stream.Write(buffer, 0, i); } } } WebResponse response = request.GetResponse(); response.Close(); 

So ... Does anyone have an opinion on whether this "PUT" method is better in a SharePoint environment than when using the embedded web service?

Now I have to say that the "PUT" method is better, since it works, and I could not get WebService to work.

Whale

+1
source share

I know this is an old thread, but it kept popping up when I was looking for a solution to the same problem.

Check out Steve Curran's answer on this topic http://social.msdn.microsoft.com/Forums/en-SG/sharepointdevelopment/thread/833e38a8-f13c-490d-8ba7-b889b6b25e38 . It seems that basically the request fails because the destination URL cannot be resolved.

(Limitations of the new user of the stack stream - cannot post more than one link. See my comment for the rest)

to stroke

+6
source share
+1
source share

your code is fine, just use the destination URL instead of the empty string. See below:

 byte[] xmlByteArray; using (MemoryStream memoryStream = new MemoryStream()) { xmlDocument.Save(memoryStream); xmlBytes = memoryStream.ToArray(); } string destinationUrl = "http://webserver/site/Doclib/UploadedDocument.xml" string[] destinationUrlArray = new string[] { destinationUrl }; FieldInformation fieldInfo = new FieldInformation(); FieldInformation[] fields = { fieldInfo }; CopyResult[] resultsArray; using (Copy copyService = new Copy()) { copyService.Credentials = CredentialCache.DefaultCredentials; copyService.Url = "http://webserver/site/_vti_bin/copy.asmx"; copyService.Timeout = 600000; uint documentId = copyService.CopyIntoItems(destinationUrl , destinationUrlArray, fields, xmlByteArray, out resultsArray); } 
+1
source share

I get the same message when I use the default credentials. Try replacing them with the following:

 copyWebService.Credentials = new NetworkCredential("Administrator", "pass", "MyDomain"); 
+1
source share

Here is some code that I wrote for a while (sorry, I had to put it together, but I hope you understand it)

  // Create a request using a URL that can receive a post. WebRequest request = WebRequest.Create("http://sharepointsite/somefile.txt"); // Set the Method property of the request to POST. request.Method = "PUT" Stream dataStream; // Set the ContentType property of the WebRequest. request.ContentType = "multipart/form-data; charset=ISO-8859-1"; byte[] byteArray = File.ReadAllBytes(@"c:\somefile.txt"); // Set the ContentLength property of the WebRequest. request.ContentLength = byteArray.Length; // Get the request stream. dataStream = request.GetRequestStream(); // Write the data to the request stream. dataStream.Write(byteArray, 0, byteArray.Length); // Close the Stream object. dataStream.Close(); // Get the response. HttpWebResponse response = (HttpWebResponse)request.GetResponse(); HttpStatusCode statCode = response.StatusCode; // Get the stream containing content returned by the server. dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. string responseFromServer = reader.ReadToEnd(); // Clean up the streams. reader.Close(); dataStream.Close(); response.Close(); 
0
source share

I'm not sure if it will solve your problem, but when referring to a web service, do not use the [URL] URLs.

Try instead: http: // [server] / _vti_bin / [webservice].

I am not an expert in SP, but I am sure that the web services belong to the main server and not to the specific site.

Hope this helps.

0
source share

I had a familiar problem, it turned out that the client was configured to use NTLM security, but the NTLM header was not attached.

In my case, because I used this code on the server side of an ASP.NET application, I had to enable Windows authentication and install

identity impersonate = "true"

in the server.web section.

0
source share

if your sharepoint server is built on a farm, check your "Alternate Access Card", see if there is an entry: yourwebserverurl intranet yourwebserverurl if not, add it.

for my case, after adding this, the copy service will start working.

This is likely due to the resolution of the farm load balance address.

0
source share

I do not understand why you are using Copy, not UpdateListItems . Perhaps UpdateListItems will be better matched?

-one
source share

All Articles