D365 CE: Get Logged in User’s Security Roles using JavaScript

Many times we come across requirements such as show/hide ribbon buttons based on logged in user’s security role.

Earlier, we used to get security roles of logged in user at client side using Xrm.Utility.getGlobalContext().userSettings.securityRoles which used to return array of GUID value of each security role.

Now that it’s deprecated, we can use Xrm.Utility.getGlobalContext().userSettings.roles which returns collection of objects with GUID and name of each security role that is assigned to the user directly or through the teams.

Below is the script which checks if the user has certain security roles based on names and hides the ribbon button if the user doesn’t have any of those security roles:

SAB.ShowHideReopenButton = function () {
    var roles = Xrm.Utility.getGlobalContext().userSettings.roles;

    if (roles === null) return false;

    var hasRole = false;
    roles.forEach(function (item) {
        if (item.name.toLowerCase() === "cs manager" || item.name.toLowerCase() === "cs administrator") {
            hasRole = true;
        }
    });

    return hasRole;
}

Hope it helps !!

3 thoughts on “D365 CE: Get Logged in User’s Security Roles using JavaScript

Leave a Reply to Nishant Rana Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.