0

I added 3 users in ClientPeoplePicker but when i am removing 1 or 2 users and saving the ClientPeoplePicker values. Still saving all 3 users. How to handle these removed users from ClientPeoplePicker? Can you suggest me.

  • share some of your code please – akbar ali Dec 29 '16 at 13:19
1

you can add validation to allow only one person to the people picker by using the following code in the submission button

  <script type="text/javascript">
    $("#btnsubmit").on("click", function(a) {
        if ($("#txtName").find('div.sp-peoplepicker-topLevel').find('.sp-peoplepicker-resolveList').children().length != 1) {
            alert("Please enter only one name");
            initializePeoplePicker("txtName"); //Reinitialise people picker again
        } else{
           saving code
        }
    });
    </script>
0

Javascript Code:

var usr;
var contextDR;
var webDR;
var teamleaderarray = new Array();
$(document).ready(function ()  {
     initializePeoplePicker('peoplePickerteamleaderDiv');
     SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getusername); 
});

//initialize PeoplePicker div tag 
function initializePeoplePicker(peoplePickerElementId)  {
     var schema = {};
     schema['PrincipalAccountType'] = 'User,DL,SecGroup,SPGroup';
     schema['SearchPrincipalSource'] = 15;
     schema['ResolvePrincipalSource'] = 15;
     schema['AllowMultipleValues'] = true;
     schema['MaximumEntitySuggestions'] = 50;
     schema['Width'] = '222px'; 
     this.SPClientPeoplePicker_InitStandaloneControlWrapper(peoplePickerElementId, null, schema);
}

function getUserInfo1()  
{
     teamleaderarray = [];
     var peoplePicker = this.SPClientPeoplePicker.SPClientPeoplePickerDict.peoplePickerteamleaderDiv_TopSpan;
     var users = peoplePicker.GetAllUserInfo();
     var userInfo = '';
     for (var i = 0; i < users.length; i++)     
     {
        var user = users[i];
        teamleaderarray.push(SP.FieldUserValue.fromUser(user["Description"]));            
     }
}

function btnSave_Click() {

    //get users from peoplepicker 
        getUserInfo1();

    //create clientcontext object and create web object     
        contextDR = SP.ClientContext.get_current();
        webDR = contextDR.get_web();
        contextDR.load(webDR);

    //get list
        var Lst = webDR.get_lists().getByTitle('listname');
        contextDR.load(Lst);        
   //create new item in list    
        var itemCreateInfo = new SP.ListItemCreationInformation();
        InListItem = Lst.addItem(itemCreateInfo);
        InListItem.set_item('Auditleader', teamleaderarray);                
        InListItem.update();    
        contextDR.load(InListItem);      
        contextDR.executeQueryAsync(Function.createDelegate(this, this.submitsuccess), Function.createDelegate(this, this.submitfail)); 
}

body part:

<div id="peoplePickerteamleaderDiv"></div>
<input type="button" id="btnSave" value="Save" onclick="javascript: btnSave_Click();" />

use this js reference:

_layouts/15/clienttemplates.js
_layouts/15/clientpeoplepicker.js
_layouts/15/clientforms.js
_layouts/15/autofill.js
_layouts/15/1033/strings.js

Using this code my peoplepicker run perfectly,

if any problem add comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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