Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers

up vote 9 down vote accepted

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();
share|improve this answer
    
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. –  Robert Kaucher Oct 12 '12 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. –  Per Jakobsen Oct 12 '12 at 20:39
    
Thanks, top one is managed client object model, though. –  Robert Kaucher Oct 12 '12 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? –  Robert Kaucher Oct 12 '12 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. –  Per Jakobsen Oct 13 '12 at 8:09
show 3 more comments

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.

share|improve this answer
    
Thank you both! I knew it had to be easier than I was making it out to be. –  Robert Kaucher Oct 12 '12 at 20:55
add comment

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();
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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