Thursday, May 19, 2016

Add items to Sharepoint having multiselect Lookup through SharePoint REST


If you are trying to add the list items in the normal way using the REST you would probably get this error

"an unexpected 'primitivevalue' node was found when reading from the json reader"

This is because you would have used the wrong format of people picker or lookup field to update or add item .Below eg "spLookup" is my lookup field and is multiselect .Sharepoint creates the field named spLookupId .


function CreateListItems(){
var data = {
    __metadata: { 'type': 'SP.Data.ThiloshListItem' },
    Title: 'Create from the REST code',  
    spLookupId : { 'results': [1,2] }
};

var url = "/_api/web/lists/getbytitle('thilosh')/items";
var siteurl = "https://thilosh1224456.sharepoint.com/sites/TeamSite"
//var test = __spPageContextInfo.webAbsoluteUrl;
$.ajax({
       // url: __spPageContextInfo.webAbsoluteUrl + url,
   url : siteurl + url,
        type: "POST",
        headers: {
            "accept": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "content-Type": "application/json;odata=verbose"
        },
        data: JSON.stringify(data),
        success: function (data) {
            $("#resultArea").append("Item added from REST code");
        },
        error: function (error) {
            alert(JSON.stringify(error));
        }
    });
}

No comments:

Post a Comment