Allison Paiva
Allison Paiva

Reputation: 37

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();

Upvotes: 1

Views: 3369

Answers (2)

HO LI Pin
HO LI Pin

Reputation: 1681

I faced same challenge in C# and I use below code to solve the problem.

public void Update_SPO_ListItemMultiplePeoplePicker(int SPO_Item_ID)
    {
        string siteUrl = "https://yourtenant.sharepoint.com/sites/subsite/";
        string ListTitle = "SharePointListTitle";
        string NewUserLookupValue = "[email protected]";

         using (var clientcontext = new AuthenticationManager().GetWebLoginClientContext(siteUrl))
        {
            // connect to sharepoint online list by lookup List name
            List list = clientcontext.Web.Lists.GetByTitle(ListTitle);
            clientcontext.Load(list);
            clientcontext.ExecuteQuery();

            // Get list item by ID.
            ListItem listItem = list.GetItemById(SPO_Item_ID);
            clientcontext.Load(listItem);
            clientcontext.ExecuteQuery();


            // get multiple people picker old value , replace [attendees] with your internal people picker field value 
            var attendees = listItem["attendees"] as Microsoft.SharePoint.Client.FieldUserValue[];

            //Initiate a FieldUserValue array which can carry additional 1 more elements ontop existing array .
            int NewArayLength = attendees.Length + 1;
            FieldUserValue[] users = new FieldUserValue[NewArayLength];

            // Loop existing multiple people picker value then saved existing old values to new FieldUserValue array 
            int i = 0;
            foreach (var attendee in attendees)
            {
                users[i] = new FieldUserValue();
                users[i].LookupId = attendee.LookupId;
                i++;
            }

            // set new user object which will be prepare add to new FieldUserValue array 
            Microsoft.SharePoint.Client.User Newattendee = clientcontext.Web.EnsureUser(NewUserLookupValue);
            clientcontext.Load(Newattendee);
            clientcontext.ExecuteQuery();


            // add new user to the last index of FieldUserValue array
            users[i] = new FieldUserValue();
            users[i].LookupId = Newattendee.Id;


            // Update list item with new  FieldUserValue array
            listItem["attendees"] = users;
            listItem.Update();
            clientcontext.ExecuteQuery();

        }

    }

Upvotes: 0

Amos
Amos

Reputation: 2091

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

Upvotes: 0

Related Questions