I am using SharePoint Online:
I have a form that has a few multi-select choice fields. When I try to add an item, I get the following error:
"value: "A node of type 'StartArray' was read from the JSON reader when trying to read a value of a property; however, a 'PrimitiveValue' or 'StartObject' node was expected."
Here's the code and the fields that are multi-select.
$.ajax({
url: fullUrl,
method: "POST",
data: JSON.stringify({
'__metadata': { 'type': 'SP.Data.AuditItem' },
'Register': that.register,
'RiskRegister': that.nextIndex,
'Reopen': that.formatDate(that.reopen),
'RiskOrIssue': that.riskOrIssue,
'Locations': that.locations, //<---multi-select choice field
'ProblemT': that.probTitle,
'ProblemStatement': that.problemStatement,
'TaskOwner': that.taskOwner,
'RiskOwner': that.riskOwner,
'Auditor': that.auditor //<--- multi-select choice field
}),
headers: {
"accept": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function(){
alert("Item Added!");
},
error: function(data){
console.log(data);
}
});
How can I solve this issue?
Thank you!
Multi-select choice field value needs to set with a collection, here is a code snippet for your reference:
<script type="text/javascript">
var locations = ['Locations1','Locations3'];
var Auditors = ['Auditor1','Auditor3'];
var item = {
"__metadata": {
"type": 'SP.Data.MyListListItem'
},
"Title":'Test',
"Locations": { '__metadata': { 'type' : 'Collection(Edm.String)'}, results: locations },
"Auditor": { '__metadata': { 'type' : 'Collection(Edm.String)'}, results: Auditors }
};
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('MyList')/items",
method: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(item),
async: false,
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function(data) {
alert('The Request has been successfully Added');
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR.responseText);
alert('Error');
}
});
</script>
Reference: