【发布时间】: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