【问题标题】:How can we update a people picker field in sharepoint online?我们如何在线更新 sharepoint 中的人员选择器字段?
【发布时间】:2020-11-11 15:18:47
【问题描述】:

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

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

【问题讨论】:

  • 也许使用 SPFieldUserValueCollection 而不是 FieldUserValue[] 并使用 .Add 方法。稍后将其保存回现场。

标签: c# sharepoint-online csom sharepoint-list peoplepicker


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    我在 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();
    
            }
    
        }
    

    【讨论】:

      猜你喜欢
      • 2020-11-13
      • 2016-10-18
      • 1970-01-01
      • 2012-08-18
      • 2017-08-06
      • 2015-02-15
      • 1970-01-01
      • 2019-12-24
      • 1970-01-01
      相关资源
      最近更新 更多