0

I've gone through quite a few blog posts, StackExhange/-Overflow questions, and also Microsoft's documentation, but I'm just unable to change the fields of a file via REST in Sharepoint running on Office 365. Even though I get HTTP 200 for most ways I've tried I haven't been able to get any values changed.

Here's the deal. I have uploaded a file via REST: File

That file has a set up of fields like this: Fields

I want to change the fields ContentTypeId, CustomerNo and CustomerName.

1. What URL can/should I use to POST the changes to?

I have used e.g. https://examplecustomer.sharepoint.com/sites/examplesite/_api/web/GetFolderByServerRelativeUrl('/sites/examplesite/Dokumenter/44')/files('testing-rest3.txt')/ListItemAllFields

2. Can I change more than one field value at the time?

I think most examples I have seen only change one value.

3. What should the JSON for changing the mentioned field values look like?

I have tried quite a few variants derived from the examples, and while most give HTTP 200 back I've yet to see any changes. Also I feel unsure if I send the correct __metadata type.

4. Which X-Http-Method should I use?

Different examples use PATCH and MERGE, but I haven't seen any explanations on what to use.

A typically REST call I have used to update a field is as follows:

  $.ajax({
        url: "https://examplecustomer.sharepoint.com/sites/examplesite/_api/web/GetFolderByServerRelativeUrl('/sites/examplesite/Dokumenter/44')/files('testing-rest3.txt')/ListItemAllFields",
        type: 'POST',
        data: JSON.stringify({
                '__metadata': {'type':'SP.Data.DokumenterItem'},
                'CustomerNo': '42'
            }),
        dataType: 'json',
        headers: {
            'Authorization': ('Bearer ' + digest),
            'Content-Type': 'application/json;odata=verbose',
            'X-Http-Method': 'PATCH'
        },
        success: function (data, textStatus, jqXhr) {
            console.log('Got HTTP status [' + jqXhr.status + '] trying to update file. :-D');
        },
        error: function (jqXhr, textStatus, errorThrown) {
            console.log('Failed to update file. Got status [' + textStatus + '] and error [' + errorThrown + '].');
        },
        timeout: 20000
    });

Note that I don't have much experience with Sharepoint at all.

0

I'm not 100% percent sure, but it seems that you need to update the list item and not the file. From the pictures above, it looks like you need to use this post url:

    $ajax({
      url: https://examplecustomer.sharepoint.com/sites/examplesite/_api/web/lists/getByTitle('Dokumenter')/items(19)",
      type: "POST",
      data: "{ '__metadata': { 'type': 'SP.Data.DokumenterItem' },
        'ContentType': 'MySpecialDocumentType'}",
      dataType: 'json',
      headers: { 
        "Authorization": ("Bearer " + digest),
        "IF-MATCH": "*",
        "content-type": "application/json;odata=verbose",
        "X-HTTP-Method": "MERGE"
      },
      success: successHandler,
      error: errorHandler
    });

Also, I would not update fields in the same operation as updating contenttype, but after updating the contenttype, I would update both fields (CustomerNo, CustomerName) in the next call.

1
  • Also, the __metadata value can be checked against the lists ListItemEntityTypeFullName property.
    – JakobN
    Jun 26 '15 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.