none
Save multiple users to list from custom people picker In Sp2013 using jquery. RRS feed

  • Question

  • HI,

    I have a custom code in .html page.this page have people picker control.

    <label>User Information </label><label> <div class="inner-wrap peoplePick"  id="peoplepickerDivInfonly" multiline="true"> </div></label>

    I used the below code to load schema for pick up the users.

    function initializePeoplePicker(peoplePickerElementId) {
            // schema to store picker properties, and set the properties.
            var schema = {};
            schema['PrincipalAccountType'] = 'User,DL,SecGroup,SPGroup';
            schema['SearchPrincipalSource'] = 15;
            schema['ResolvePrincipalSource'] = 15;
            schema['AllowMultipleValues'] = false;
    if(peoplePickerElementId == "peoplepickerDivInfonly")
    {
            schema['AllowMultipleValues'] = true;
    }
            schema['MaximumEntitySuggestions'] = 50;
            schema['Width'] = '176px';
            // Render and initialize the picker.
            this.SPClientPeoplePicker_InitStandaloneControlWrapper(peoplePickerElementId, null, schema);

        } 

     var peoplepickrDivInfonly = this.SPClientPeoplePicker.SPClientPeoplePickerDict.peoplepickerDivInfonly_TopSpan;
             var users11 = peoplepickrDivInfonly.GetAllUserInfo();

    var userids = {};

     var context;

    if (users11.length > 0) 
    {
    for(var i=0;i<users11.length;i++)
    {
    userids.user11 = context.get_web().ensureUser(users11[i].Key);
    context.load(userids.user11);
    Infogrp.push(userids.user11);
    }

    }

    under context :

    // i have to load all controls.

    var strinfo="";
    if (users11.length > 0)
    {
    for(var j=0;j< Infogrp.length;j++)
    {
        strinfo += Infogrp[j].get_id()";";
    finalusers.push(strinfo);
    }
    }

    if(Hitcount == 0 && lastUniqueID =="")
    { //many fields but i am enterd 3 only.
    var batchScore = "<Method ID='1' Cmd='New'><Field Name='Title'>" + reqnum + "</Field>\
    <Field Name='DocumentType'>" + doctyp + "</Field>\

    <Field Name='InformationOnly'>" + finalusers + "</Field>\

    <Field Name='ApprovalStatusLog'>" + apprstatlog + "</Field></Method>";

    }

     var soapEnv = "<?xml version=\'1.0\' encoding=\'utf-8\'?> \
    <soap:Envelope xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\' \
    xmlns:xsd=\'http://www.w3.org/2001/XMLSchema\' \
    xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/\'> \
    <soap:Body> \
    <UpdateListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
    <listName>"+ listname +"</listName> \
    <updates> \
    <Batch OnError='Continue'>" + batchScore + "</Batch> \
    </updates> \
    </UpdateListItems> \
    </soap:Body> \
    </soap:Envelope>";
                   
    $.ajax({
    url: HRConstants.ServiceUrl,
    contentType: "text/xml; charset=utf-8",
    beforeSend: function (xhr) {
    xhr.setRequestHeader("SOAPAction",
    "http://schemas.microsoft.com/sharepoint/soap/UpdateListItems");
    },
    type: "POST",
    asyc: false,
    dataType: "xml", 
    data: soapEnv,
    complete: Result,
    error : addRequestFailed
    });

    i changed the column in list  as multiple values set as true.when i click on save button this field have save the all user(multi selection) data to list column.

    i used soap format (As batch) to insert the item into list.But i am unable to save this.

    Please help.


    Wednesday, March 1, 2017 10:43 AM

All replies

  • Hi sudhir,

    We can save the multiple users from client people picker like below using JavaScript Client Object Model:

    var list = web.get_lists().getByTitle("StudentDetails");
    var listCreationInformation = new SP.ListItemCreationInformation();
    var listItem = list.addItem(listCreationInformation);
    var peoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict.peoplePickerDiv_TopSpan;
    // Get information about all users.
    var users = peoplePicker.GetAllUserInfo();
    // Get user keys.
    var keys = peoplePicker.GetAllUserKeys();
    var finalusers = new Array();
    for (var i = 0; i < users.length; i++) {
    var arryuser = users[i];
    finalusers.push(SP.FieldUserValue.fromUser(arryuser.Key));
    }
    listItem.set_item("Supervisor", finalusers);
    listItem.update();
    ctx.load(listItem);

    More information:

    Insert Multiple values for Person/Group Field using JSOM

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Thursday, March 2, 2017 8:12 AM