1

I am getting this error while trying to post data to SharePoint list. It has one lookup field PostCategory.

{"readyState":4,"responseText":"{\"error\":{\"code\":\"-1, Microsoft.SharePoint.Client.InvalidClientQueryException\",\"message\":{\"lang\":\"en-US\",\"value\":\"An unexpected 'PrimitiveValue' node was found when reading from the JSON reader. A 'StartObject' node was expected.\"}}}","responseJSON":{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"An unexpected 'PrimitiveValue' node was found when reading from the JSON reader. A 'StartObject' node was expected."}}},"status":400,"statusText":"error"}

I have created another lookup field but there I was able to update by rest, but not able to update to the previous field.

enter image description here

PostCategory is the exisitng field and I have added Newcategory field. Both are lookup field, but there is some difference as there is no results array in NewCategory. I think may be this is the reason I am not able to post data to PostCategory field.

    $.ajax({  
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('Posts')/items(1)",  
    type: "POST",  
    headers: {  
        "accept": "application/json;odata=verbose",  
        "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
        "content-Type": "application/json;odata=verbose",  
        "IF-MATCH": "*",  
        "X-HTTP-Method": "MERGE"  
    },  
    data: "{__metadata:{'type':'SP.Data.PostsListItem'},PostCategoryId: 4}",    
    success: function(data) {  
        console.log(data.d.results);  
    },  
    error: function(error) {  
        console.log(JSON.stringify(error));  
    }  
});  

1 Answer 1

3

If your column's internal name is PostCategory and list's internal name is Posts then try using below code as your data in POST call:

data: JSON.stringify({
        "__metadata": { type: "SP.Data.PostsListItem" },
        "PostCategoryId": 2
      })

You can get the exact internal name of your column by following this article: How to find the Internal name of columns in SharePoint Online?

Is multiple selection allowed on PostCategory?

Update:

For multiple selection field you need to pass an object with a results property, that contains an array of the IDs (number). Here is an example:

data: JSON.stringify({
        "__metadata": { type: "SP.Data.PostsListItem" },
        "PostCategoryId": { "results": [2, 4] }
      })
5
  • still geting the same error, multiple selection is not allowed
    – KumarV
    Jul 22, 2019 at 9:17
  • Is there any other column in list which is required and its default value is not set? Jul 22, 2019 at 9:34
  • Can you please add screenshot of PostCategory column settings? Jul 22, 2019 at 9:41
  • It seems, this PostCategory is multi selection lookup field, As i could not select more than one option in form so I thought its not a multi select field.
    – KumarV
    Jul 22, 2019 at 9:51
  • For multiple selection, please see updated answer. Jul 22, 2019 at 9:55

Your Answer

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

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