Jquery – Sharepoint: How to update a ListItem field which is Lookup multiple values using Sharepoint RestAPI

jqueryrestsharepointsharepoint-2013

Sharepoint: How to update a ListItem field which is Lookup multiple values using Sharepoint RestAPI?

I want to update a ListItem field, this field type is Lookup multiple value. I code the sample test but it doesn't work.

Here is my code:

 function UpdateItemLookup() {
    // Getting our list items
    $.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('testlist')/Items(1)",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        cache: true,
        async: false,
        success: function (data) {
            Update(data);
        },
        error: function (data) {
            console.log(data);
        }
    });
}

function Update(result) {
    $.ajax({          
        url: result.d.__metadata.uri,
        type: "POST",
        contentType: "application/json;odata=verbose",
        body: JSON.stringify({
            "__metadata": { type: "SP.Data.Test_x0020_listListItem" },
            Title: "bbbb",
            lookup1: [1, 3] //the lookup field needs to be updated to "1" and "3" 
        }),
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "content-type": "application/json;odata=verbose",
            "X-HTTP-Method": "MERGE",
            "If-Match": result.d.__metadata.etag
        },
        success: function (result) {
            console.log(result);
        },
        error: function (error) {
            console.log(error);
        }
    });
}

I get this error:

"{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"A node of type 'EndOfInput' was read from the JSON reader when trying to read the start of an entry. A 'StartObject' node was expected."}}}"

The lookupfield internalname is correct.
enter image description here

Error:
enter image description here

Anyone here can solve my problem? Many thanks.

Best Answer

I finally find the error. The field name is correct "lookup1", but when I update the value of this field, I need to add postfix "Id" after the field name. My case should be "lookup1Id' and it works.

 function Update(result) {
    var item = $.extend({
        "__metadata": { "type": "SP.Data.Test_x0020_listListItem" }
    }, {
        Title: "test item aaaa",
        lookup1Id: { "results": [1, 3] }, //here is the problem, it should be "lookup1Id"
    });

    $.ajax({            
        url: result.d.__metadata.uri,
        type: "POST",
        contentType: "application/json;odata=verbose",
        data: JSON.stringify(item),
        headers: {
            "Accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "X-HTTP-Method": "MERGE",
            "If-Match": result.d.__metadata.etag
        },
        success: function (result) {
            console.log(result);
        },
        error: function (error) {
            console.log(error);
        }
    });
}
Related Topic