SharePoint CSOM:如何使用 ValidateUpdateListItem 更新个人或组字段?

SharePoint CSOM: How to update person or group field using ValidateUpdateListItem?

我正在使用客户端对象模型的 ValidateUpdateListItem 方法更新 SharePoint 列表项,以防止创建新的项目版本。这基本上适用于所有字段,除了具有个人或组字段类型的字段。有谁知道用作 ListItemFormUpdateValue 对象的 FieldValue 的用户或组值的正确字符串表示形式是什么?我已经尝试了所有对我来说合理的方法(用户信息中的用户 ID、登录名、查找值,如这些数据的组合等),但没有成功。

不幸的是ListItem.ValidateUpdateListItem method does not support the update of user field value。例如,在下面的示例中 AssignedTo 字段将不会更新:

using (var ctx = GetContext(webUri, userName, password))
{
     var list = ctx.Web.Lists.GetByTitle(listTitle);
     var item = list.GetItemById(itemId);

     var formValues = new List<ListItemFormUpdateValue>();
     formValues.Add(new ListItemFormUpdateValue() { FieldName = "Title", FieldValue = taskName});
     formValues.Add(new ListItemFormUpdateValue() { FieldName = "AssignedTo", FieldValue = userId.ToString() });  //not supported
     item.ValidateUpdateListItem(formValues, true, string.Empty);
     ctx.ExecuteQuery();

}

而是考虑 ListItem.Update Method 更新用户字段值,如下所示:

using (var ctx = GetContext(webUri, userName, password))       
{

      var list = ctx.Web.Lists.GetByTitle(listTitle); 
      var item = list.GetItemById(itemId);

      item["Title"] = taskName;
      var assignedToValue = new FieldUserValue() { LookupId = userId };
      var assignedToValues = new[] { assignedToValue };
      item["AssignedTo"] = assignedToValues;  //multi-valued user field
      item.Update();
      ctx.ExecuteQuery();
}

我刚刚 运行 遇到一个问题,即使用项目更新更新超过 12 个人员或组字段会导致抛出异常。显然,这是由于 SP 在线列表视图查找阈值(截至该日期为 12)引起的。

http://blog.vanmeeuwen-online.nl/2012/07/value-does-not-fall-within-expected.html

为了解决这个问题,我使用了 ValidateUpdateListItem 方法来更新个人或组 ID。诀窍是用

的格式给它分配一个 json

[{"Key":"i:0#.f|membership|user@yoursite.onmicrosoft.com"}]

 formValues.Add(new ListItemFormUpdateValue() { FieldName = "AssignedTo", FieldValue = "[{'Key':'i:0#.f|membership|user@yoursite.onmicrosoft.com'}]" });

对于多个值,可以用逗号分隔。没有在小组中尝试过,但我认为它应该有效。

希望这对某人有用。