Get User Roles With Webresource In Dynamics CRM

Introduction

In Dynamics 365 CRM, for certain requirements we need to get user roles so that based on those roles we can show or hide a few fields or some other core logic. As an example, on selected contact record client API user settings present in Xrm.Utility namespace was used.

Step 1

Login to the required environment and select required solution [Contact Customizations Solution in this case] as shown in the below figure.

Get User Roles with Webresource in Dynamics CRM

Step 2

After Step 1, select contact web resource in solution and click on Edit as shown in the below figure.

Get User Roles with Webresource in Dynamics CRM

Step 3

After Step 2, the below code is used to get roles that are present for the logged-in user

let roles = Xrm.Utility.getGlobalContext().userSettings.roles;

Step 4

After Step 3, keep the below code under a method and declare a variable to hold role name that was used to verify and a flag to hold whether the user have given role or not using the below code.

function checkRoles(executionContext) {
    let formContext = executionContext.getFormContext();
    if (formContext !== null && formContext != 'undefined') {
        let roleName = "Basic User";
        let hasRole = false;
        let roles = Xrm.Utility.getGlobalContext().userSettings.roles;
        roles.forEach(x => {
            if (x.name === roleName) {
                hasRole = true;
                return;
            }
        });
        if (hasRole) {
            var confirmStrings = {
                text: "User has Role -" + roleName,
                title: "Roles Information"
            };
            var confirmOptions = {
                height: 200,
                width: 450
            };
            Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(function(success) {
                if (success.confirmed) console.log("Dialog closed using OK button.");
                else console.log("Dialog closed using Cancel button or X.");
            });
        }
    }
}

Step 5

After Step 4, final code looks like below

if (typeof(ContosoVaccination) == "undefined") {
    var ContosoVaccination = {
        __namespace: true
    };
}
if (typeof(ContosoVaccination.Scripts) == "undefined") {
    ContosoVaccination.Scripts = {
        __namespace: true
    };
}
ContosoVaccination.Scripts.ContactForm = {
    handleOnLoad: function(executionContext) {
        console.log('on load - contact form');
        checkRoles(executionContext);
    },
    __namespace: true
}

function checkRoles(executionContext) {
    let formContext = executionContext.getFormContext();
    if (formContext !== null && formContext != 'undefined') {
        let roleName = "Basic User";
        let hasRole = false;
        let roles = Xrm.Utility.getGlobalContext().userSettings.roles;
        roles.forEach(x => {
            if (x.name === roleName) {
                hasRole = true;
                return;
            }
        });
        if (hasRole) {
            var confirmStrings = {
                text: "User has Role -" + roleName,
                title: "Roles Information"
            };
            var confirmOptions = {
                height: 200,
                width: 450
            };
            Xrm.Navigation.openConfirmDialog(confirmStrings, confirmOptions).then(function(success) {
                if (success.confirmed) console.log("Dialog closed using OK button.");
                else console.log("Dialog closed using Cancel button or X.");
            });
        }
    }
}

Step 6

After Step 5, save the code and publish the Webresource and open any contact record and observe the popup for the role details as shown in the below figure.

Get User Roles with Webresource in Dynamics CRM

Note:

  1. Make sure to publish all customizations and upload JavaScript (js) file.
  2. You can write your logic based on the flag to show few fields and hide few fields for users based on the given requirement.
  3. Microsoft Documentation link can be found here

Conclusion

In this way, one can easily get roles of the user using Webresource(javascript).


Similar Articles