Set SharePoint's PeoplePicker value with jQuery

PeoplePicker and default values - not so easy in SharePoint.
So if you want a default value for a field you need to set it by your own, but how?


You can do it easily by using the little function below.
You can call it e.g. at document.ready.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
function fillUserField(fieldName, accountName) {
    // get the id of the top element
    var peoplePickerPersonTopId = $("div[id^='" + fieldName + "'][id$='ClientPeoplePicker']").attr('id');
    // fill the editor input with the account name (the hidden input will be filled automatically)
    $("input[id^='" + fieldName + "'][id$='ClientPeoplePicker_EditorInput']").val(accountName);
    // sp.js is necessary so make sure it is loaded
    ExecuteOrDelayUntilScriptLoaded(function (){
        // get a poplepicker object
        var peoplePickerOject = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplePickerPersonTopId];
        // resolve the account name (this step filles the hidden input)
        peoplePickerOject.AddUnresolvedUserFromEditor(true);
    }, "sp.js");
}

Comments

Popular posts from this blog

Refresh the User Information List in SharePoint

Get the mail address from a FieldUserValue Client Side vs Server Side