12

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?

0

3 Answers 3

14

According 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();
8
  • Isn't that managed Client Object Model? My question is regarding JavaScript. Also, what I am really having an issue with is that this must be multiple users. Oct 12, 2012 at 20:19
  • 1
    This is JavaScript CSOM you can see that from all the 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
  • Thanks, top one is managed client object model, though. Oct 12, 2012 at 20:51
  • Per, thanks for being such an awesome contributor to this community. Any way I can show my gratitude for all times you've answered my questions? Oct 12, 2012 at 21:13
  • 1
    Regarding showing gratitude. You just did. Appart from that I prefer you to "pay it forward". And if we meet at a Conference you can buy me a Coke Zero. Oct 13, 2012 at 8:09
3

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.

1
  • Thank you both! I knew it had to be easier than I was making it out to be. Oct 12, 2012 at 20:55
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.