Wednesday, 19 November 2014

SharePoint 2013: setting People Picker Value in NewForm.aspx

SharePoint 2013, people picker supports auto complete. You will not see the address book and the resolve user button any more on the new form.aspx.  If you are looking for a short cut key to resolve the user you can still use Cntrl + K  key combination.

SharePoint 2013 people picker.
In order to provide support for people picker in apps, Microsoft has come up with three new files as given below and is located in the 15 hive.
  1. clientform.js
  2. clientpeoplepicker.js
  3. autofill.js
The above 3 files contains most of the functionality required to be performed by people picker.
Here is the code to fill a people picker in SP2013 with a User.
function SetAndResolvePeoplePicker(fieldName, userAccountName) {
    var controlName = fieldName;
    var peoplePickerDiv = $("[id$='ClientPeoplePicker'][title='" + controlName + "']");
    var peoplePickerEditor = peoplePickerDiv.find("[title='" + controlName + "']");
    var spPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplePickerDiv[0].id];
    peoplePickerEditor.val(userAccountName);
    spPeoplePicker.AddUnresolvedUserFromEditor(true);           
     //disable the field

     spPeoplePicker.SetEnabledState(false);

        //hide the delete/remove use image from the people picker

        $('.sp-peoplepicker-delImage').css('display', 'none');

   }
In the above method, the parameters:
fieldName: Name of the people picker column
userAccountName: user name (ex: domain\username).

Hope this Posts helps. Please comment below if you have better solution.