SharePoint 2013 更新多个用户字段(Person or Group)

有时我们需要更新一个用户到Person or Group类型的字段, 当然这个属性允许设置多个用户, 要如何才能添加新的用户到该字段,同时还不影响原始存在的值。

这里我们需要了解 SPFieldUserValueCollection类, 该类是包含user字段的所有值,而SPFieldUserValue继承了SPFieldLookupVlaue.

 1 // 获取List
 2 SPListItemCollection BGProfileCollection = SPContext.Current.Web.Lists[CSTListType.BGProfileListName].Items;
 3 
 4 // 根据条件去获取需要更新的某一条数据
 5 SPListItem BGProfileListItem = BGProfileCollection.OfType<SPListItem>().Where(a => a.GetLookupField<int>(ConstVariables.AllBGProfileColumns.ColumnBGName, 0, 0) == Convert.ToInt32(this.ddl_BGNameList.SelectedItem.Value))..FirstOrDefault();
 6 
 7 // 获取该条数据user字段的值
 8 SPFieldUserValueCollection values = (SPFieldUserValueCollection)BGProfileListItem ["MultiUser"];
 9 
10 //添加用户到该集合, 这里我使用当前用户
11 values.Add(new SPFieldUserValue(SPContext.Current.Site.OpenWeb(), SPContext.Current.Web.CurrentUser.ID, SPContext.Current.Web.CurrentUser.Name));
12 
13 // 更新该字段的值
14 BGProfileListItem["MultiUser"] = values;
15 BGProfileListItem.Update();
原文地址:https://www.cnblogs.com/mystar/p/4810474.html