Setting AssignedToId from JavaScript / JSON
Maybe someone already blogged about this but I could not find it.
I needed to create new tasks in a tasks list on my SharePoint 2013 site but I could not get it to accept a value for the AssignedToId as I wanted to not only create the task but also assign it to the appropriate people.
This field is a multi-value field where multiple people can be assigned to the task. He you have to provide a JSON array of user IDs.
// add new task to task list
var requestUri = _spPageContextInfo.webAbsoluteUrl +
"/_api/Web/Lists/getByTitle('Test Tasks')/items/";
var requestHeaders = {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
}
var requestData = {
__metadata: { "type": "SP.Data.Test_x0020_TasksListItem" },
'Title': “Do something" productive”,
'Body': “Do something productive and help the team succeed”,
'AssignedToId': {"results":[11]}, // user id
'DueDate': “12/30/2013”
};
var requestBody = JSON.stringify(requestData);
$.ajax({
url: requestUri,
type: "Post",
contentType: "application/json;odata=verbose",
headers: requestHeaders,
data: requestBody,
success: onSuccess,
error: onError
});
Comments
- Anonymous
October 23, 2013
visit http://codebeautify.org/ for json and other scripting languages.