Sharepoint - SharePoint 2013 REST - Create list item with user field and lookup field
It depends whether User/Lookup field value is multiple or not, the following formats are used when specifying field value via SharePoint REST:
- Single User/Lookup value format:
FieldName: LookupIdVal
- Mutiple User/Lookup value format:
FieldName: {"results": [LookupIdVal1,LookupIdVal2] }
How to set multiple lookup/user field value via SharePoint REST API
var listName = 'Tasks';
var itemProperties = {
'__metadata': { "type": "SP.Data.TasksListItem" },
"Title": 'New task',
'AssignedToId': {"results": [12] } //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);
}
});
How to set single lookup/user field value via SharePoint REST API
var listName = 'Tasks';
var taskProperties = {
'__metadata': { "type": "SP.Data.TasksListItem" },
"Title": 'New task',
'AssignedToId': 12 //single-valued User field value
};
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",
type: "POST",
contentType: "application/json;odata=verbose",
data: JSON.stringify(taskProperties),
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);
}
});