问题
I am trying to upload a document to a document library for Sharepoint in iOS, and having limited success.
I have used Copy.CopyIntoItems to at least get my Document to appear in the target document library, using this soap request:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<CopyIntoItems xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<SourceUrl>[server url]</SourceUrl>
<DestinationUrls><string>[server url]</string></DestinationUrls>
<Fields></Fields>
<Stream>[base 64 encoded string]</Stream>
</CopyIntoItems>
</soap:Body>
</soap:Envelope>
This gets me to at least have my documents appear in my library. However, the response doesn't give me any metadata about them (like GUID, etc.), which makes it harder to manage on my end. Additionally, when viewing the item in a browser, I have additional options for actions, like "View Source Item", and when deleting an extra prompt stating "This item was copied from another location...". As it is mostly the Bobs who use SharePoint, this will only serve to confuse them. Ideally, the document's actions would look the same as when uploading directly through the document library in a browser. The more transparency is better.
The other common solution I've seen online has been to use a combination of Lists.UpdateListItems and Lists.AddAttachment. I've gotten Lists.UpdateListItems to work properly and create a new entry (and this is nice because it gives me metadata back like GUID, and at least at first glance appears the same as a form-uploaded document). However, Lists.AddAttachment won't work for me. Using this SOAP request for the AddAttachment (The GUID is the newly added item's GUID with UpdateListItems):
<?xml version="1.0" ?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<AddAttachment xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<listName>Test</listName>
<listItemID>{1F214D95-B92B-4E10-8B96-4B04DC6DA9B6}</listItemID>
<fileName>screenshot.png</fileName>
<attachment>[base 64 string]</attachment>
</AddAttachment>
</soap:Body>
</soap:Envelope>
I get the following response:
<?xml version="1.0" ?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<soap:Fault>
<faultcode>
soap:Server
</faultcode>
<faultstring>
Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.
</faultstring>
<detail>
<errorstring xmlns="http://schemas.microsoft.com/sharepoint/soap/">
Input string was not in a correct format.
</errorstring>
</detail>
</soap:Fault>
</soap:Body>
</soap:Envelope>
Any ideas? I'm sure I'm going down the wrong path somewhere, I'm just not sure where. Thanks!
回答1:
Got it working this morning. I ended up doing the simplest approach, which I was honestly never expected would work with a Microsoft API.
NSString *fullPath = @"https://site/library/folders/document.txt";
NSURL *url = [NSURL URLWithString: fullPath];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"PUT";
request.HTTPBody = [NSData {documentData}];
//this is the authentication Cookie acquired in a previous request.
[request addValue:cookie forHTTPHeaderField:@"Cookie"];
[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
The key seems to be using PUT as the method.
It's important to note, unfortunately, that this suffers from the problem above of limited meta-data returned, but because the behavior is so similar to uploading directly through the a SharePoint web site, I decided it was okay. I'm probably just going to rework the logic where I decide whether to download/update an existing document or not.
来源:https://stackoverflow.com/questions/10076804/upload-document-to-sharepoint-library-using-low-level-web-services