Person or Group field in SharePoint is similar to a Lookup field. When you are updating this field using REST API, you need to append "Id" to the name of the column in the body construct. For example, the body construct looks like this:
The highlighted portions should be replaced by the actual List Name and Column Name. In the above example, the REST call is updating a List item with Title and People columns.
How to get the value for user ID ("4" in the above example) needs a separate explanation and that will be my next post!
The above example works fine if Person field is configured to accept only one value. If we change the Person field to accept multiple values, how do we pass more than one value in the REST call? Since we normally separate user names with semicolon in people picker, I tried separating the values similarly in the above construct something like this:
Code gracefully threw an error as
When I looked at JSON response from "GET" request, things became clearer.
If Person field is set to accept only one value, I can fetch the value of Person field with the below object hierarchy:
If Person field is set to accept more than one value, it changes to something like this:
Notice that, in the case of multiple users, it stores as a collection within the column. So when we are forming the JSON, we need to form the values as a subset. This is how it is changed now.
This time, item was created successfully with two users in the column.
data: { "__metadata": { "type": "SP.Data.ListNameListItem" }, "Title": "First Item", "PeopleFieldId": "4" };
The highlighted portions should be replaced by the actual List Name and Column Name. In the above example, the REST call is updating a List item with Title and People columns.
How to get the value for user ID ("4" in the above example) needs a separate explanation and that will be my next post!
The above example works fine if Person field is configured to accept only one value. If we change the Person field to accept multiple values, how do we pass more than one value in the REST call? Since we normally separate user names with semicolon in people picker, I tried separating the values similarly in the above construct something like this:
data: { "__metadata": { "type": "SP.Data.ListNameListItem" }, "Title": "First Item", "PeopleFieldId": "4; 7" };
Code gracefully threw an error as
An unexpected 'PrimitiveValue' node was found when reading from the JSON reader. A 'StartObject' node was expected
When I looked at JSON response from "GET" request, things became clearer.
If Person field is set to accept only one value, I can fetch the value of Person field with the below object hierarchy:
response.d.results[0].PeopleFieldId
If Person field is set to accept more than one value, it changes to something like this:
response.d.results[0].PeopleFieldId.results[0]
, results[0] will have ID of first user, and results[1] will have ID of second user, and so on.Notice that, in the case of multiple users, it stores as a collection within the column. So when we are forming the JSON, we need to form the values as a subset. This is how it is changed now.
data: { "__metadata": { "type": "SP.Data.ListNameListItem" }, "Title": "Second Item", "PeopleFieldId": { "results": ["4", "7"] } };
This time, item was created successfully with two users in the column.
Hey, please let us know if you got chance to write a post of how you got 4 in above example?
ReplyDeleteI have similar situation where I need to get specific users ID and pass it while creating item through REST
Cheers
@salah saleh, you can get the user id from _spPageContextInfo.userId property. This will return the user ID of the logged in user.
DeleteJust to make it more clearer: (I am sure it will save someone sometime)
ReplyDeletevar requestApprover = {
'__metadata' : { 'type': 'Collection(Edm.Int32)' },
'results': [10,20,30]
};
Remember the field name should contain Id at the end and the type should be the field type that you are updating where for people or groups (multiple) is Collection(Edm.Int32).
Wow, it saved my time, many thx, why novbody ellse mentioning that fieldname should end with ID????
DeleteNot sure how you get this working I am getting below mention error
ReplyDelete{
"status": 400,
"message": "Invalid JSON. An unexpected comma was found in scope 'Property'. A comma is only valid between properties of an object or between elements of an array.\r\nclientRequestId: df784485-ac72-4880-ac45-8b1716e3c498\r\nserviceRequestId: 2b72839e-0080-6000-28a8-52736ddc3bdb",
"source": "https://xxxxx.sharepoint.com/xxxx/_api/web/lists/GetByTitle('Product%20Transition%20Tracker')/items(6)",
"errors": [
"-1",
"Microsoft.SharePoint.Client.InvalidClientQueryException"
]
}
Body is
{
'__metadata':{
'type':'SP.Data.ProductTransitionTrackerListItem'}, 'Title':'Updated',
'SupplyChainPlannerId':,{ 'results': ['1940', '1636'] },
'DemandPlannerId':'2573'
}
Thank you!!!
ReplyDeleteUgh... Microsoft!
Hi, Where is the post to get the UserIDs? I have a requirement. I have a list where in one item field, I have multiple people added. My requirement is to copy this to another list. SO, if 2 people are added into an item, how will get their IDs in the Flow so that I can copy them to a different list
ReplyDeleteHow to add null or no values to People or Group column
ReplyDeletejust set empty []
DeleteSample code:
let spItem = new spItem(); // CDRId:number[]=[];
spItem.CDRId = (formEnteredData.cdReview)? this.getPeoplePickerArr(formEnteredData.cdReview):[];
private getPeoplePickerArr = (users: IPeoplePickerUser[]): number[] => {
let spSelectedUsers: number[] =[];
users.forEach(peoplePickerUser => {
spSelectedUsers.push(peoplePickerUser.Id);
});
return spSelectedUsers;
}
//set multivalue peoplepicker
CDRId': { 'results': spItem.CDRId } ,
just set empty []
DeleteSample code:
let spItem = new spItem(); // CDRId:number[]=[];
spItem.CDRId = (formEnteredData.cdReview)? this.getPeoplePickerArr(formEnteredData.cdReview):[];
private getPeoplePickerArr = (users: IPeoplePickerUser[]): number[] => {
let spSelectedUsers: number[] =[];
users.forEach(peoplePickerUser => {
spSelectedUsers.push(peoplePickerUser.Id);
});
return spSelectedUsers;
}
//set multivalue peoplepicker
CDRId': { 'results': spItem.CDRId } ,
I have tried with 'results': [0]. But it is giving error
ReplyDelete'results': []
Delete