1

I have a very basic custom list which contains a few People Picker fields. The very first people picker column is called "Name", and I want it to be filled automatically with the current logged user when opens NewForm.aspx

The rest of the other People Picker fields shall be manually filled, My need is only to populate the very first field value.

I am using SharePoint Online O365, which means only JavaScript solutions will work for me with Script Editor on NewForm.aspx.

I have this script below which gets me the value of the current logged user and populates into the first people picker:

<script type="text/javascript">
    $(document).ready(function () {
        ExecuteOrDelayUntilScriptLoaded(setLoginName, "clientpeoplepicker.js");
    });

    function setLoginName() {
        var loginName = _spPageContextInfo.userLoginName;
        var peoplePickerDiv = $("[id$='ClientPeoplePicker'][title='Name']");
        var peoplePickerEditor = peoplePickerDiv.find("[title='Name']");
        var spPeoplePicker = SPClientPeoplePicker.SPClientPeoplePickerDict[peoplePickerDiv[0].id];
        peoplePickerEditor.val(loginName);
        spPeoplePicker.AddUnresolvedUserFromEditor(true);
    }
 </script>

But, I am quite unsatisfied with it because it does not work properly with other web browsers as you can see the screenshot below it is very fuzzy value in Edge and Chrome:

enter image description here

Could you please help me out? Is there any better code or change I could do to get better result in all browsers?

1 Answer 1

0

Because people picker control is rendered an-sync by js so when you set value to control, it is not rendered. You must set some delay time to wait for control rendering.

You can use SPService.js, it's published in codeplex

In my case, I used bellowing code

function setUserFieldValue(fieldName, loginName) {
    function _setUserFieldValue() {
        var _PeoplePicker = jQuery("div[title='" + fieldName + "']");
        if (_PeoplePicker == undefined || _PeoplePicker.length == 0) {
            _PeoplePicker = jQuery("div[title='" + fieldName + " Required Field']");
        }
        var _PeoplePickerTopId = _PeoplePicker.attr('id');
        if (SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId] === undefined) {
            setTimeout(_setUserFieldValue, 200); //delay in 0.2 second until control is rendered
        }
        else {
            var _PeoplePickerEditer = jQuery("input[title='" + fieldName + "']");
            if (_PeoplePickerEditer == undefined || _PeoplePickerEditer.length == 0) {
                _PeoplePickerEditer = jQuery("input[title='" + fieldName + " Required Field']");
            }
            _PeoplePickerEditer.val(loginName);
            var _PeoplePickerOject = SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId];
            _PeoplePickerOject.AddUnresolvedUserFromEditor(true);
        }
    }
    function waitcorejs() {
        ExecuteOrDelayUntilScriptLoaded(_setUserFieldValue, "core.js");
    }
    ExecuteOrDelayUntilScriptLoaded(waitcorejs, "clientpeoplepicker.js");

}

7
  • coc2coc, I tried to use your code but maybe I am missing something, Could you please correct me? My people picker field called "Name" which I used it to replace it with your: " Required Field' . Also, I added: <script type="text/javascript" src="ajax.googleapis.com/ajax/libs/jquery/1.3.2/…> But, It didn't work Commented Sep 19, 2016 at 18:09
  • @MaryLarson: in case you want to set user contoso\user01 to field Name, you call setUserFieldValue('Name', 'contoso\user01')
    – coc2coc
    Commented Sep 19, 2016 at 18:13
  • I would prefer to see the full display name or the email address (Not: Domain\Account), But, Could you please guide me how to use your code - Am I missing anything ? Commented Sep 19, 2016 at 18:16
  • Hi Mary, my code will display name in people picker based on parameter loginName. It's a function, so you can call it in $(document).ready like this $(document).ready(function () { setUserFieldValue('Name', _spPageContextInfo.userLoginName) });
    – coc2coc
    Commented Sep 19, 2016 at 18:20
  • I managed to call the function using your last hint -Thanks! I noticed that this code only works in IE not Edge nor chrome. Also, On IE itself it will prompt a Security Dialog: "Show Secure Content", Is this normal behavior? Commented Sep 19, 2016 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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