如何使用 SharePoint REST API 设置用户字段值
假设以下函数用于使用 SharePoint REST 创建列表项:
function createListItem(webUrl,listName,itemProperties)
{
return $.ajax({
url: webUrl + "/_api/web/lists/getbytitle('" + listName + "')/items",
type: "POST",
processData: false,
contentType: "application/json;odata=verbose",
data: JSON.stringify(itemProperties),
headers: {
"Accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
}
});
}
用户字段值的格式:
- 单值用户字段:
'<user field name>' : <user id>
- 多值用户字段:
'<user field name>' : { 'results': [<array of user ids>] }
多用户字段值
该示例演示了如何创建任务项并指定multi-valued AssignedTo
field:
//Create a Task item
var taskProperties = {
'__metadata' : { 'type': 'SP.Data.TasksListItem' },
'Title': 'Order approval',
'AssignedToId' : { 'results': [10] }
};
createListItem(_spPageContextInfo.webAbsoluteUrl,'Tasks',taskProperties)
.done(function(data)
{
console.log('Task has been created successfully');
})
.fail(
function(error){
console.log(JSON.stringify(error));
});
单个用户字段值
该示例演示了如何创建任务项并指定single-valued AssignedTo
field:
//Create a Task item
var taskProperties = {
'__metadata' : { 'type': 'SP.Data.TasksListItem' },
'Title': 'Order approval',
'AssignedToId' : 10
};
createListItem(_spPageContextInfo.webAbsoluteUrl,'Tasks',taskProperties)
.done(function(data)
{
console.log('Task has been created successfully');
})
.fail(
function(error){
console.log(JSON.stringify(error));
});