c0deNinja
c0deNinja

Reputation: 3986

Saving a Person or Group field using REST

Does anyone know how to save a Person field using REST?

I have tried the following and it works:

{ 
   "__metadata": { "type": "SP.Data.SomeListListItem" } , 
   "MyPersonFieldId" : 1
}

But this only works if you know the ID. I don't have that! How can I get it? I have the key which is i.0#w|domain\userName.

I tried the following and it doesnt work either:

{ 
   "__metadata": { "type": "SP.Data.SomeListListItem" } , 
   "MyPersonField" : { "__metadata": { "type": "SP.Data.UserInfoItem" }, "Name": "i.0#w|domain\userName" }
}

Any ideas?? Thanks!

Upvotes: 4

Views: 10303

Answers (3)

Juan Amador
Juan Amador

Reputation: 633

The thing is that User information is a lookup field thereby MyPersonField does not exist on your SharePoint list if you use an OData endpoint, I really don't know how to save data but same problem happened to me when I tried to read a user.

for example {server}/{api}/list/getbytitle('mylist')/items does not return MyPersonField instead return MyPersonFieldId.

But if we use:

{server}/{api}/list/getbytitle('mylist')/items/?$select=*,MyPersonField/Name&$expand=MyPersonField

We are able to work with MyPersonField lookup values.

Upvotes: 0

Sreedhar
Sreedhar

Reputation: 1

Use the below code to get Current User ID to save user under People and group column. People column name is Requestor. But to save user we have to specify column name as RequestorId

var userid = _spPageContextInfo.userId; // To get current user ID
var itemProperties={'Title':vTitle,'RequestorId':userid};

Upvotes: 0

Chris Quick
Chris Quick

Reputation: 111

I haven't done this with a Person field, but I did do something similar with a managed metadata field. I basically had to pass in additional information as an object to create the value in the field.

See if passing in the ID of the user along with the name works. I'm about to try this myself as I have the same need.

{
     "MyPersonField": { "Name": "i.0#w|domain\userName", "ID": 1 }
}

EDIT: Ok, updating this field is easier than I thought. I was able to perform the update by simply passing in the ID of the user to the Id field:

{
     "MyPersonFieldId": 1
}

This means the user should already be in the site collection, so if the user doesn't exist the request will fail.

Upvotes: 5

Related Questions