sharepointupdatescsomsplistitemspuserfield

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


I am updating a SharePoint list item using the ValidateUpdateListItem method of the client-side object model to prevent creation of a new item version. This basically works fine for all fields except the ones with person or group field type. Does anyone know what is the correct string representation of a user or group value to be used as FieldValue of an ListItemFormUpdateValue object? I have already tried everything that seems reasonable to me (user ID from User Info, login name, lookup-value like combinations of these data etc.) without any success.


Solution

  • Unfortunately ListItem.ValidateUpdateListItem method does not support the update of user field value. For example, in the following example AssignedTo field will not be updated:

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

    Instead consider ListItem.Update Method to update user field value as demonstrated below:

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