Question

I'm trying to insert a new task in a standard SharePoint task list, this is my item:

var item =  {
    Title: taskTitle + " - " + subSiteName ,
    ProjectId: projectID,
    AssignedToId: {'results': [15, 17]} 
};

POSTing returns an error 400.

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

Can anyone help me? Thanks in ahead.

Was it helpful?

Solution

Ensure that allow multiple selections is checked

enter image description here

Also, make sure your json is as below:

var data = {
    __metadata: { "type": "SP.Data.TestListItem" },
    Title: "Some title",
    ProjectId: projectID,
    AssignedToId: { 'results': [15,17] } 
}

Full code:

var listName = 'Tasks';


var itemProperties = {
    '__metadata': { "type": "SP.Data.TasksListItem" },
    "Title": taskTitle + " - " + subSiteName ,  
    "ProjectId": projectID, 
    'AssignedToId': {"results": [15,17] }  //multi-valued User field value 
};

$.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",
    type: "POST",
    contentType: "application/json;odata=verbose",
    data: JSON.stringify(itemProperties),
    headers: {
        "Accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    success: function (data) {
        console.log(JSON.stringify(data.d));
    },
    error: function (data) {
        console.log(data.responseText);
    }
});

Reference - Create task list item with REST API

OTHER TIPS

Please try below code.

  var taskProperties = {
        '__metadata' : { 'type': 'SP.Data.TasksListItem' },
        'Title': 'Order approval',
        'AssignedToId' : { { "results": ["4", "7"] } }
    };
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top