有时我们需要更新一个用户到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();

 

相关文章:

  • 2021-12-03
  • 2021-06-27
  • 2021-10-04
  • 2021-12-23
  • 2022-01-23
  • 2021-12-21
猜你喜欢
  • 2022-01-13
  • 2021-12-23
  • 2021-06-27
  • 2022-02-05
  • 2021-07-02
  • 2021-12-25
相关资源
相似解决方案