Search code examples
c#sharepoint-onlinecsomsharepoint-listpeoplepicker

How can we update a people picker field in sharepoint online?


Trying to update a people picker field in SP online list.I'm able to update with single value, but when I try to update with new value in the filed, the existing value is being removed. I need to append the new input value, with existing values in that field. How can this be achieved?

List list = ctx.Web.Lists.GetByTitle("ListName");
ListItem targetListItem = list.GetItemById(ItemID);
ctx.Load(targetListItem);
ctx.ExecuteQuery();
                
FieldUserValue[] userValueCollection = new FieldUserValue[1];

//Get all the users of this Web
User user = ctx.Web.SiteUsers.GetByEmail(emailIdOfUser);
ctx.Load(user);
ctx.ExecuteQuery();

if (user != null)
{
    FieldUserValue fieldUserVal = new FieldUserValue();
    fieldUserVal.LookupId = user.Id;
    userValueCollection.SetValue(fieldUserVal, 0);
}
FieldUserValue[] existingUsers = targetListItem["PeoplePickerColumnName"] as FieldUserValue[];
List<int> userValues = new List<int>();
foreach (FieldUserValue x in existingUsers)
{
    userValues.Add(x.LookupId);
    int counts = userValues.Count();
}
targetListItem["PeoplePickerColumnName"] = userValueCollection;
targetListItem.Update();
ctx.ExecuteQuery();

Solution

  • You could read the old value and then update the column with all values which you want to set in column.

    You could get a update multi selection user field demo here:

    https://social.msdn.microsoft.com/Forums/office/en-US/900b5143-f5b3-4fd5-a9ce-3e7d7c3ecfc1/csomjsom-operation-to-update-user-field-value?forum=sharepointdevelopment