我们如何在线更新 Sharepoint 中的人员选择器字段?

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

尝试在线更新 SP 中的人员选择器字段 list.I能够使用单个值进行更新,但是当我尝试使用字段中的新值进行更新时,现有值将被删除。我需要将新的输入值附加到该字段中的现有值。如何实现?

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

您可以读取旧值,然后使用要在列中设置的所有值更新列。

您可以在此处获取更新的多选用户字段演示:

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

我在 C# 中遇到了同样的挑战,我使用下面的代码来解决这个问题。

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

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

        }

    }