0

I want to set a value (person) in the SharePoint PeoplePicker using JavaScript / jQuery.

The PeoplePicker is in the div which is called "Author".

How can I trigger the event which resolves the SharePoint User and displays the appropriate User if it is available or the error message if the user was not found?

If I use jQuery and try to find the "img" element and then call the function "click()" nothing happens.

Is there another way to call the function which activates the SharePoint behavior?

2
//Works with SharePoint 2010
            $("div[title='People Picker']").text(strUserName);

//Works with SharePoint 2013
            try {
                //SharePoint 2013
                var travelPeoplePicker = null;
                for (var prop in SPClientPeoplePicker.SPClientPeoplePickerDict) {
                    if (prop.indexOf("YOUR_PEOPLEPICKER_FIELD_NAME_HERE") >= 0) {
                        travelPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[prop];
                        break;
                    }
                }
                if (travelPeoplePicker != null) {

                    var usrObj = { 'Key': strUserName };
                    travelPeoplePicker.AddUnresolvedUser(usrObj, true);
                    //$("input[title='TravelRequestor']").focus().val(strUserName).attr("size", strUserName.length);
                }
            }
            catch (e) {
                //alert(e);
            }

Answer reference from Set default value in people picker javascript/jquery

0
    $(document).ready(function ()
    {
        initializePeoplePicker('Author');
    });

    //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);
    }

    //get peoplePicker value on save
    function getUserInfo() {
        AuthorUserArray = [];
        var peoplePicker = this.SPClientPeoplePicker.SPClientPeoplePickerDict.Author_TopSpan;
        var users = peoplePicker.GetAllUserInfo();
        var userInfo = '';
        for (var i = 0; i < users.length; i++) {
            var user = users[i];
            AuthorUserArray.push(SP.FieldUserValue.fromUser(user["Description"]));
        }
    }

    //button save function
    function btn_save()
    {
        ListItem = Lst.getItemById(id);
        ListItem.set_item('Author', AuthorUserArray);
        ListItem.update();
        context.load(UpListItem);
    }
0

You can use the logIn ID or the Email of the user if you have and put it in this code.

 var peoplePicker =this.SPClientPeoplePicker.SPClientPeoplePickerDict.yourPPLpickerDivNameHere;
 var usrObj = { 'Key': loginIdOrEmail };
 peoplePicker.AddUnresolvedUser(usrObj,true); 

credit/reference : http://ranaictiu-technicalblog.blogspot.com/2014/11/sharepoint-2013-client-side-people.html

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.