Get displaytext from people picker

Hi,

I'm setting up a form, which gets the current user and populates a field with their firstname+lastname, using your myProperties.get() code from Populate SharePoint form fields with profile information — SharePoint forms.

It also gets their manager, and puts that into a (single entry) people picker, fd,field('LineManager').value.

I'm then trying to get the fd.field('LineManager').value.DisplayText and copy it into a text field (LineManagerName), to display the name there and hide the manager people picker field (so it can't be manually changed), but it gives 'undefined'.

case 'Manager':
    MgrName = props[i].Value;
    break;

Later in the function...

fd.field('LineManager').value = MgrName;
fd.field('LineManagerName').value = fd.field('LineManager').value.DisplayText;
alert(fd.field('LineManager').value.DisplayText);
alert(fd.field('LineManagerName').value);

Both alerts show 'undefined'.

Why won't it get the displaytext? What am I doing wrong?

I did try to just disable the LineManager people picker, but it seems to ignore the code, even though it's the first line in Rendered:

fd.spRendered(function() {
    fd.field('LineManager').disabled = true;

Can you help me get the displaytext for the manager?

Regards
Nick

Dear @Nick.Jones,
Can you share the whole code that you're using, or at least all the relevant parts together? It's really hard to say from small pieces of code what goes wrong as I'm not even sure which parts work and which don't.

1 Like

Hi @Nikita_Kurguzov

This is the code for the New form, some of it is commented out because a lot of the fields will initially be hidden and I need to see them for testing:

function newUserInfo() { //for new entries
    var AllFName = "";
    var AllLName = "";
    var AllBothName = "";
    var MgrName = "";
    var AllUName = "";
    var AllPURL = "";
    var AllText = "";
        
    pnp.sp.profiles.myProperties.get().then(function(result) { //get current user info
        var props = result.UserProfileProperties;

        
        for (var i = 0; i < props.length; i++) {
            switch (props[i].Key) {
                case 'FirstName':
                    AllFName = props[i].Value + " "; //add space to first name for formatting
                    break;

                case 'LastName':
                    AllLName = props[i].Value;
                    break;

                case 'Manager':
                    MgrName = props[i].Value;
                    break;

                case 'PictureURL':
                    AllPURL = props[i].Value;
                    break;

                case 'UserName':
                    AllUName = props[i].Value;
            }
        }
        
        fd.field('TestLine').value = "This is a test";

        AllBothName = AllFName + AllLName;
        alert("New bothname");
        alert(AllBothName);
        
        AllText = " " + AllBothName + "'s ";
        $(fd.control('RichText1').$el).html(AllText);

        fd.field('LineManager').value = MgrName;	
        fd.field('LineManagerName').value = fd.field('LineManager').value.DisplayText;

        alert("New Mgr");
        alert(MgrName);
        alert("New LM1");
        alert(fd.field('LineManager').value);
        alert("New LM2");
        alert(fd.field('LineManager').value.DisplayText);
        alert("New LMN");
        alert(fd.field('LineManagerName').value);
        
        fd.field('Grower').value = AllUName; //won't show until after save
        fd.field('Title').value = AllBothName;

        fd.control('Image1').source = "https://ourcompany.sharepoint.com/sites/CompanyHR/SiteAssets/SitePages/HR-Form/" + AllBothName + ".jpg";
        alert(fd.control('Image1').source);

        $(fd.control('Image7').$el).hide();   //header for manager response area
        //$(fd.field('LineManager').$parent.$el).hide();  //person field to use for manager name
        //$(fd.field('ManagerResponse').$parent.$el).hide();    //response from manager

    });
}

fd.spRendered(function() {

    fd.field('LineManager').disabled = true;
    //$(fd.field('LineManager').$parent.$el).hide();
    
    //hide the extraneous fields except when testing
    //$(fd.field('Title').$parent.$el).hide();  //text version of full name
    //$(fd.field('Grower').$parent.$el).hide(); //person field, not shown until saved.
    
    newUserInfo();
    alert("After New");

    alert("1 New grower");
    alert(fd.field('Grower').value);
    alert("2 New Title");
    alert(fd.field('Title').value);

    //There's some assignments for field content here, but it's all working fine.
});

Apologies, it's a bit of a mess because I've been trying different methods.

Regards
Nick

Dear @Nick.Jones,
It takes time for Person field to process assigned value, so the Display text is not readily available.

Instead, try the following approach:

var MgrName = "";
pnp.sp.profiles.myProperties.get().then(function(result) { //get current user info
        var props = result.UserProfileProperties;
        console.log(props);
        for (var i = 0; i < props.length; i++) {
            switch (props[i].Key) {
                case 'Manager':
                    MgrName = props[i].Value;
                    break;
            }
        }
        fd.field('Person').value = MgrName;	
});

fd.field('Person').$on('change',function(value){
  fd.field('Title').value = value.DisplayText;
});
1 Like

Hi @Nikita_Kurguzov ,

Many thanks, it looks like that's worked!

Regards
Nick