Sandrooco Sandrooco - 4 months ago 72
jQuery Question

Sharepoint 2013 Rest API: create to multivalue column

I'm trying to save some values to a multi value field, more precisely a survey list column. My attempts:

$.ajax({
url: "somesitecollection/_api/web/lists/getByTitle('survey')/items",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify({
'__metadata': {
'type': 'SP.Data.SurveyListItem'
},
'Question1': {
"__metadata": {
"type": "Collection(SP.FieldRatingScaleQuestionAnswer)"
},
"results": [2, 3, 5]
},
'Comment': "Thanks for..."
}),
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},

success: function (data) {
console.log("done");
},
error: function (err) {
console.log(err.status + " - " + err.responseJSON.error.message.value);
}
});


I think there's something wrong with the "results" part in the "Question1" JSON, don't really find the problem though.

Error message:


"400: An unexpected 'PrimitiveValue' node was found when reading from
the JSON reader. A 'StartObject' node was expected."


What does that mean?

Update



Tried with this code:

$.ajax({
url: "somesitecollection/_api/web/lists/getByTitle('survey')/items",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify({
'__metadata': {
'type': 'SP.Data.SurveyListItem'
},
'Question1': {
"__metadata": {
"type": "Collection(SP.FieldRatingScaleQuestionAnswer)"
},
"results": [
{
"Answer": 1
}, {
"Answer": 5
}, {
"Answer": 4
}
]
},
'Comment': "Thanks for..."
}),
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
},

success: function (data) {
console.log("ok");
},
error: function (err) {
console.log(err.status + " - " + err.responseJSON.error.message.value);
}
});


No errors now. Only the comment is saved though. Any idea?

Answer

A while after posting I found the solution. You have to give the question too:

"results": [
   {
      "Answer": 1,
      "Question": "Question 1"
   }, {
      "Answer": 5,
      "Question": "Question 2"
   }, {
      "Answer": 4,
      "Question": "Question 3"
   }
]
Comments