How to update a person field using spHttpClient
A few things to note:
- Before doing the actual update you must ensure the user Id of the user, this is what you will input as the field value.
- You must add Id to your field name. If the field name is
Personyou should use PersonIdin the update body.
private updatePeopleField(username: string, listname: string, itemid: string) {
let client = this.props.context.spHttpClient;
let clientconfig = SPHttpClient.configurations.v1;
let options:ISPHttpClientOptions = {
body: JSON.stringify({'logonName' : username})
};
client.post("/_api/web/ensureuser", clientconfig, options).then((res:SPHttpClientResponse) => {
res.json().then(json => {
return json.Id;
}).then(userid => {
let body= JSON.stringify({
'__metadata': { 'type': `SP.Data.${listname}ListItem` },
'PersonId': userid});
let updateoptions = {
headers: {
'Accept': 'application/json;odata=nometadata',
'Content-type': 'application/json;odata=verbose',
'odata-version': '',
'IF-MATCH': '*',
'X-HTTP-Method': 'MERGE'
},
body: body
};
client.post(`/_api/web/lists/getByTitle('${listname}')/items('${itemid}')`, clientconfig , updateoptions)
.then((response: SPHttpClientResponse) => {
console.log('Item updated!');
});
});
});
}