0

With the follwing code, I am trying to update a list. Title succesfully passes but PerOrBus does not.

<script>
    $('#submitdata').click(function(){

    var itemProperties = {
        '__metadata': { "type": "SP.List" },
        Title: $("#firstName").val(),
        PerOrBus: "Yes"

    };



    $.ajax({
         url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('ChangeOfAddressList')/items",
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(itemProperties),
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        },
        success: function (data) {
            console.log(JSON.stringify(data.d));
        },
        error: function (data) {
            console.log(data.responseText);
        }
    });
});


</script> 

I get this error on the debugger:

"value":"The property 'PerOrBus' does not exist on type 'SP.List'. Make sure to only use property names that are defined by the type."

Why would it update Title and not PerOrBus? PerOrBus is a checkbox so I've tried doing PerOrBus:"False" as well as making it true and null but none of them worked.
Any Ideas? Thanks

5
  • Try using 1 or 0 Commented May 5, 2016 at 16:00
  • I tried that to no avail. However I think I need to declare it boolean somewhere maybe?
    – Stacker
    Commented May 5, 2016 at 16:05
  • Are you sure that's the internal name? REST uses internal names, so if there was a space or it was renamed after creation, it could have a different internal name than the display name.
    – Mike
    Commented May 5, 2016 at 16:23
  • Hi Mike, That is the column name. Straight from /items url: <d:ContentTypeId>0x010000E29694B8D7EEF4EAEDAD5D400494DC</d:ContentTypeId><d:Title>c\s</d:Title><d:PerOrBus m:type="Edm.Boolean">true</d:PerOrBus><d:FirstName m:null="true" /><d:Surname m:null="true" /><d:BusName m:null="true" /><d:AccountsID m:null="true" /><d:coaCountry m:null="true" /><d:coaAddressL1 m:null="true" /><d:coaAddressl2 m:null="true" /><d:rmfj m:null="true"
    – Stacker
    Commented May 5, 2016 at 16:26
  • thats a blank submission I got populating Title before I added PerOrBus to it
    – Stacker
    Commented May 5, 2016 at 16:27

1 Answer 1

2

Two things:

  1. Your metadata type is wrong. It should be SP.Data.ChangeOfAddressListListItem. The format is SP.Data.(list name)ListItem or SP.Data.(library name)Item when adding or updating an item.
  2. You are missing some headers. A POST request signals an add but you want to do an update. To tell SharePoint that you really want to do an update you need to send the X-Http-Method header with a value of MERGE or PATCH. You should also send an IF-MATCH header to tell SharePoint how to do concurrency checking. I'm not sure if this header is optional or not. Finally, you should add a Content-Type header to tell SharePoint the schema of the message body.

Here is the code with these changes applied.

<script>
    $('#submitdata').click(function(){

    var itemProperties = {
        '__metadata': { "type": "SP.Data.ChangeOfAddressListListItem" },
        Title: $("#firstName").val(),
        PerOrBus: "Yes"

    };



    $.ajax({
         url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('ChangeOfAddressList')/items",
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(itemProperties),
        headers: {
            "Accept": "application/json;odata=verbose",
            "Content-Type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "IF-MATCH": "*",
            "X-Http-Method": "PATCH"
        },
        success: function (data) {
            console.log(JSON.stringify(data.d));
        },
        error: function (data) {
            console.log(data.responseText);
        }
    });
});


</script> 

Here's a working example from one of my conference talks:

function updateItem() {
    UpdateFormDigest(_spPageContextInfo.webServerRelativeUrl, _spFormDigestRefreshInterval)

    var call = jQuery.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('Tasks')/Items/?$top=1",
        type: "GET",
        dataType: "json",
        headers: {
            Accept: "application/json;odata=verbose"
        }
    });
    call.done(function (data, textStatus, jqXHR) {
        var items = data.d.results;
        if (items.length > 0) {
            var item = items[0];
            updateItem(item);
        }
    });
    call.fail(failHandler);

    function updateItem(item) {
        var call = jQuery.ajax({
            url: _spPageContextInfo.webAbsoluteUrl +
                "/_api/Web/Lists/getByTitle('Tasks')/Items(" +
                item.Id + ")",
            type: "POST",
            data: JSON.stringify({
                "__metadata": { type: "SP.Data.TasksListItem" },
                Status: "In Progress",
                PercentComplete: 0.10
            }),
            headers: {
                Accept: "application/json;odata=verbose",
                "Content-Type": "application/json;odata=verbose",
                "X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
                "IF-MATCH": item.__metadata.etag,
                "X-Http-Method": "PATCH"
            }
        });
        call.done(function (data, textStatus, jqXHR) {
            var div = jQuery("#message");
            div.text("Item updated");
        });
        call.fail(failHandler);
    }
}

You can download the slide deck and the samples for the talk from http://ow.ly/4mRlUc

7
  • BTW, I cover this and more in my Client Object Model and REST API course on Pluralsight. Commented May 5, 2016 at 16:34
  • hi Rob, Thanks. I tried this and got the following on the chrome debugger: SP.ListItemEntityCollection does not support HTTP PATCH method."
    – Stacker
    Commented May 5, 2016 at 16:37
  • Ah, yes, I missed that. You haven't specified which item you are updating. Your URL needs to be something like ".../getbytitle('ChangeOfAddressList')/items(" + itemId + ")" Commented May 5, 2016 at 16:41
  • I've added a second code example from one of my conference talks for you to reference. Commented May 5, 2016 at 16:47
  • So if I'm updating 5 items in one go I'd do something like 'ChangeOfAddressList')/items(" + FirstName + ")( " + Surname + ") etc? Your link is blocked on my network unfortunately
    – Stacker
    Commented May 6, 2016 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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