I have a JavaScript array containing the string representation of some usernames (domain\username format).
Using the JavaScript CSOM, what is the best way to create a new item and add those usernames to that new item's multiple value user field?
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.
Sign up to join this communityAccording to How to set people and group column programmatically using Client Object Model this is how you set single user fields:
SP.User _newUser = _ctx.Web.EnsureUser("domain\\username");
_ctx.Load(_newUser);
_ctx.ExecuteQuery();
SP.FieldUserValue _userValue = new SP.FieldUserValue();
_userValue.LookupId = _newUser.Id;
_listItem["userfield"] = _userValue;
_listItem.Update();
_ctx.ExecuteQuery();
According to Add multiple people using Javascript Client Object Model the following should work for Multi user fields:
var users = new Array();
users.push(SP.FieldUserValue.fromUser("testAias1"));
users.push(SP.FieldUserValue.fromUser("testAias2"));
item.set_item(columnName, users);
item.update();
SP.
which isn't used in managed CSOM. I've added some multi user code. You may have to mix the two samples.
Oct 12, 2012 at 20:39
The Multiple User field is internally just a LookUp Field. The following code should work:
var users = new Array();
users.push(SP.FieldUserValue.fromUser("userName1"));
users.push(SP.FieldUserValue.fromUser("userName2"));
item.set_item(fieldName, users);
item.update();
Instead of SP.FieldUserValue.fromUser("userName1")
you can use your existing SP.FieldUserValue
object. Also make sure that the column is set to accept multiple users as input. You can set this in the column settings.
The above solution didn't work for me. It is described here: Add multiple people using Javascript Client Object Model to a Field of Type SPFieldUserValue
This is what worked for me:
var users = User1_ID + ';#' + User1_Name + ';#' + User2_ID + ';#' + User2_Name;
listItem.set_item(fieldName, users); listItem.update();