Show Only First Name in Custom Message

Hi,

I have this code which displays the Logged in User's Full Name:

fd.control('RichText1').$el.innerHTML = '<p style="text-align:Justify;font-style:italic;font-size:24px;color:#FF0000;">Hello ' + _spPageContextInfo.userDisplayName + '!</p>'

How can I get it to display only their First Name?

Thank you!

Dear @Qman,
You can try to retrieve the First Name property of the current user with pnpjs, some examples for this are here - Retrieve user profile information — SharePoint forms

It should work, if the user has FirstName in their properties:

function getFirstName() {
    pnp.sp.profiles.myProperties.get().then(function(result) {
        var props = result.UserProfileProperties;
        for (var i = 0; i < props.length; i++) {
            if(props[i].Key == 'FirstName') {
                fd.control('RichText1').$el.innerHTML = '<p style="text-align:Justify;font-style:italic;font-size:24px;color:#FF0000;">Hello ' + props[i].Value + '!</p>'
                break;
            }
        }
    });
}

fd.spRendered(function() {
    //executes updateCurrentUserInfo on form load
    getFirstName();
});
1 Like

Thanks very much!

Your missing a curly bracket in the code:

function getFirstName() {
    pnp.sp.profiles.myProperties.get().then(function(result) {
        var props = result.UserProfileProperties;
        for (var i = 0; i < props.length; i++) {
            if(props[i].Key == 'FirstName') {
                fd.control('RichText1').$el.innerHTML = '<p style="text-align:Justify;font-style:italic;font-size:24px;color:#FF0000;">Hello ' + props[i].Value + '!</p>'
                break;
                }
        }
    });
}


    //executes updateCurrentUserInfo on form load
    getFirstName();
    
});
1 Like

Dear @Qman,
Glad to help! And I've added the missing bracket to not confuse future users :slight_smile:
Thank you for pointing it out!

1 Like