Pre-populate a people picker field with the current user's manager's name

  • 16 January 2015
  • 10 replies
  • 408 views

Badge +4

I found this link and have followed it on a form that have created using Nintex:

 

Nintex Forms: Automatically populating the new people picker control - Wow, So SharePoint, Much information

 

But for some reason it's not populating the field at all.

 

Any idea why this isn't working?  I got this working before implementing Nintex (but not sure how I can incorporate this into Nintex Form, by using the following code:

 

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script src="/siteassets/scripts/sputility.min.js"></script>

<script>

// wait for the window to load

$(window).load(function () {

     var userid = _spPageContextInfo.userId;

 

function GetUserLogin() {

var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties/";

var requestHeaders = { "accept" : "application/json;odata=verbose" };

 

$.ajax({

  url : requestUri,

  contentType : "application/json;odata=verbose",

  headers : requestHeaders,

  success : QuerySuccess,

  error : QueryError

});

}

 

function QuerySuccess(data, request){

  var loginName = data.d.DisplayName;

        var myManager = data.d.UserProfileProperties.results[15].Value;

  //$("div[title='Employee Name']").val(loginName);

  SetAndResolvePeoplePicker("Employee Name",loginName);

  SetAndResolvePeoplePicker("Approval Manager",myManager);

}

 

 

function QueryError(error) {

  alert(error);

}

 

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

    }

 

GetUserLogin();

 

});

</script>


10 replies

Badge +2

Hello Soulon,

I don't know if you still need this - However!

Tom's solution worked well for me in SP2013 and Nintex Forms 2013 2.3.4

I did need to follow the instructions exactly - the order of includes was important:

And entering the CSS class was important:

Download Tom's Libraries from his link - reference them correctly - and then it works!

Badge +4

Hi John,

I can’t seem to get this to work on Office 365 though. I feel that I’m missing something, but still can’t seem to figure it out.

Thanks for the response, I will look into what you have.

Badge +4

Hi Cellou,

I'm looking at the code now and trying out, but I'm getting the following error:

"Object doesn't support property or method '_additem' "

Badge +8

Suolon Hu​, did you manage to solve this?

Can't you use the userProfileLookup function for this purpose?

  • Add a calcualted value field
  • Dig into the properties and define your function with userProfileLookup(Current User, "Manager").
  • Test out your form

You could use the "Current User" from the common section, otherwise you could create a variable. If you get the login name back from your manager, you could store the result in a form variable and use the userProfileLookup(ManagerVariable, "PreferredName") function to populate.

Goodluck!

Badge +4

Hi Glenda,

No, I never got this to work.

I don't see the userProfileLookup function for Nintex Forms for Office 365.

Badge +2

Here is how I did it....

  1. Add a Calculate Value to the form, and create a rule to hide it.
  2. Add this formula to the Calculated control: userProfileLookup( userProfileLookup(  Current User, "Manager"), "PreferredName")
  3. Add the javascript below to the form, change the control IDs (peoplecontrol, calculatedvaluecontrol) to your controls.
  4. The 750ms delay gives the Calculated control enough time to make the 2 userProfileLookup calls, 750 was a safe number and is not perceivable by users. You may have to increase it if you have a slow network, test test test.

  

NWF$(document).ready(function()

{

   NWF.FormFiller.Events.RegisterAfterReady(function()

    {

     setTimeout(function(){  

     var ins = new NF.PeoplePickerApi('#' + peoplecontrol);

     var currentuser = NWF$('#' + calculatedvaluecontrol).val();

     ins.search(currentuser).done(function (data)

     {      

        ins.add(data[0]);

      });

}, 750);

Badge +9

Hi Alex, I take it your solution was for a on-premises version as the runtime userProfileLookup is not available in Office 365.

Badge

Oh, sorry, I didn't read this was for 0365.

Userlevel 7
Badge +17

Hi! Just pinging to find out whether you found a solution or still looking for it? If yes (if you found) - can you write here how you achieve that?

 

Regards,

Tomasz

Badge +4

Thanks for this Alex, I have been trying for ages to copy the current user to a people picker on edit form and your code has helped. BTW your code is missing 2x closing brackets }); });

Reply