Back to Basics # 59: 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 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.

Step 2:

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

Step 3:

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

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

As shown in the below figure

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

                                });

                        }

    }

}

As shown in the below figure

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

                                });

                        }

    }

}

And as shown in the below figure.

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.

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

One thought on “Back to Basics # 59: Get User Roles with Webresource in Dynamics CRM

  1. Pingback: Back To Basics : Curated List of Articles in a Single Page – Common Man Tips for Power Platform, Dynamics CRM,Azure

Leave a 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 )

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