none
Add multiple people using Javascript Client Object Model

    Question

  • I am trying to add multiple people to a SP column of type Person/Group i.e. people picker. I am able to add one successfully using their userId, but HAVE no clue how to do that for multiple people. Here is the code for one user:

    function UserDrop(e, toElement, listGuid, columnName) {
    
        
    //EcmaScript Client Object Model
        var ctx = new SP.ClientContext.get_current();
        var list = ctx.get_web().get_lists().getById(listGuid);
        var item = list.getItemById(elementId);
    //columnName is of type person/group and I am adding user //whose userId is 7
        item.set_item(columnName, 7);
        item.update();
    
        // asynchronous call
        ctx.executeQueryAsync(
            function () { toElement.innerHTML = userLinkHtml; },
    	    function () {alert ("Error")}
            );
    
        return false;
    }

    This works great and I can add user whose userId is 7, however I want to add multiple people like let's say users of user Ids 7 and 8. 

    Any ideas or help will be greatly appreciated. 

    There is a thread on this one but that's from .net COM which could accessed here: http://social.msdn.microsoft.com/Forums/en-US/sharepoint2010general/thread/5183e87c-ee1d-4594-9492-0dfdf6616cce


    7929

    Friday, April 6, 2012 5:09 PM

Answers

  • Following code worked:

        var users = new Array();
        users.push(SP.FieldUserValue.fromUser("testAias1"));
        users.push(SP.FieldUserValue.fromUser("testAias2"));
    
        item.set_item(columnName, users);
        item.update();

    Note: Make sure 'testAlias1' and 'testAlias2' are valid users. 

    get2Pallav: If you could figure out why that way wasn't working, PLEASE let me know. Looks like must work, but didn't. 


    7929

    • Marked as answer by PremShrestha Friday, April 6, 2012 9:21 PM
    Friday, April 6, 2012 9:21 PM
  • Hi,

    You can try by this .This worked for me :

    var userField = this.user1.get_id() + ";#" + this.user1.get_loginName()+";"+this.user2.get_id() + ";#" + this.user2.get_loginName();
    listItem.set_item('UserField', userField);
    listItem.Update();
    list.Update();

    Thursday, June 19, 2014 9:07 AM

All replies

  • I am trying to add multiple people to a SP column of type Person/Group. I am able to add one successfully using their userId, but HAVE no clue how to do that for multiple people. Here is the code for one user:

    function UserDrop(e, toElement, listGuid, columnName) {
    
        
    //EcmaScript Client Object Model
        var ctx = new SP.ClientContext.get_current();
        var list = ctx.get_web().get_lists().getById(listGuid);
        var item = list.getItemById(elementId);
    //columnName is of type person/group and I am adding user //whose userId is 7
        item.set_item(columnName, 7);
        item.update();
    
        // asynchronous call
        ctx.executeQueryAsync(
            function () { toElement.innerHTML = userLinkHtml; },
    	    function () {alert ("Error")}
            );
    
        return false;
    }
    

    This works great and I can add user whose userId is 7, however I want to add multiple people like let's say users of user Ids 7 and 8. 

    Any ideas or help will be greatly appreciated. 


    7929

    Wednesday, April 4, 2012 6:24 PM
  • Client Object model question is Off Topic here. It is pre sharepoint 2010 forum and just wait until this move to sharepoint 2010 forum.

    Moderator: Please move this thread to below forum:

    http://social.msdn.microsoft.com/Forums/en-US/sharepoint2010programming/threads


    Cheers, Hemendra-MCTS "Yesterday is just a memory,Tomorrow we may never see"

    Friday, April 6, 2012 6:27 AM
    Moderator
  • I have already moved this one. Thanks for letting me know. 

    7929

    • Marked as answer by PremShrestha Friday, April 6, 2012 5:12 PM
    • Unmarked as answer by PremShrestha Friday, April 6, 2012 5:12 PM
    Friday, April 6, 2012 5:10 PM
  • I have moved it here: http://social.msdn.microsoft.com/Forums/en-US/sharepoint2010programming/thread/2c113538-cbbf-4337-8cf5-9c406f9012cb

    I am not sure how to close this thread, someone please suggest how to proceed in correct manner. 

     

    7929

    Friday, April 6, 2012 5:13 PM
  • Try as follows 

    var users = new Array();
    
    users.push(SP.FieldUserValue.set_lookupId(7));
    users.push(SP.FieldUserValue.set_lookupId(8));
    ..
    
    item.set_item(columnName, users);


    get2pallav
    Please click "Propose As Answer" if this post solves your problem or "Vote As Helpful" if this post has been useful to you.

    Friday, April 6, 2012 6:42 PM
  • That looks like a extremely appealing answer. 

    However, the line

    users.push(SP.FieldUserValue.set_lookupId(7));

    is not running as the functions stops executing as soon as it's above line. I looked up the MSDN and seems to match the syntax. 

    I am fairly new to JCOM so may be a stupid question..I am not explicitly including the file SP.js, however, all other COM related syntax runs fine beside SP.FiledUserValue...any idea? 


    7929

    Friday, April 6, 2012 8:29 PM
  • Following code worked:

        var users = new Array();
        users.push(SP.FieldUserValue.fromUser("testAias1"));
        users.push(SP.FieldUserValue.fromUser("testAias2"));
    
        item.set_item(columnName, users);
        item.update();

    Note: Make sure 'testAlias1' and 'testAlias2' are valid users. 

    get2Pallav: If you could figure out why that way wasn't working, PLEASE let me know. Looks like must work, but didn't. 


    7929

    • Marked as answer by PremShrestha Friday, April 6, 2012 9:21 PM
    Friday, April 6, 2012 9:21 PM
  • Hi ,


    Can somebody let me know how the same(assigning the array values to lookup value field) can be achieved with multi-select lookup value. SP.FieldLookUpValue do not have any such methods like fromUser. Please help. Please find my code block below

    clientContext = new SP.ClientContext.get_current();
            if (this.clientContext != undefined && clientContext != null) {
    
                    var webSite = clientContext.get_web();
                    oList  = webSite.get_lists().getByTitle("Add New User");
    				$.urlParam = function(name){
    	            var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
              	    return results[1] || 0;
    		      }
                    var itemid = parseInt($.urlParam('ID')); 
        		    var item = oList.getItemById(itemid);
        		    var users = new Array();
    
    users.push(SP.FieldLookupValue.set_lookupId(1));
    users.push(SP.FieldLookupValue.set_lookupId(2));
    item.set_item('Responsibility', users); 
    item.update();
    		        clientContext.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));            
        }
    
    also i cant use managed client object model.. so pls let me know how to achieve the same using javascript client object model




    Ranjani.R

    Thursday, June 21, 2012 9:54 AM
  • Thanks PremShrestha,

    That worked for me!!

    This is the code that works in Javascript!

    Thanks,

    Shaiju


    shaiju

    Monday, July 29, 2013 3:13 PM
  • Hi,

    You can try by this .This worked for me :

    var userField = this.user1.get_id() + ";#" + this.user1.get_loginName()+";"+this.user2.get_id() + ";#" + this.user2.get_loginName();
    listItem.set_item('UserField', userField);
    listItem.Update();
    list.Update();

    Thursday, June 19, 2014 9:07 AM
  • Hi ,

    I am working on office 365 SharePoint Env, I am trying to use your above code,could you please tell me how you get the 'testAias1' and 'testAias2'. I am using people picker which is created by JSOM (ref : http://msdn.microsoft.com/en-us/library/office/jj713593(v=office.15).aspx ). i want to insert multiple users to custom list. can you help me with that.

    Thanks

    Rahul Dayal

    Saturday, July 12, 2014 6:34 AM