Friday, August 22, 2025

Read/Get The Dynamics 365 CRM/CE Logged In User Security Role Using Java Script

Below code are used to read/get the logged in user security roles.

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


Example:
Requirements:
Read the logged in user Security Roles and check the user is having Basic User and Sales manager roles, If have then show the Contact Job Title field otherwise hide it.


function checkRoles(executionContext) 
{
    let formContext = executionContext.getFormContext();
    let hasRole = false;
    let roles = Xrm.Utility.getGlobalContext().userSettings.roles;
    // ["AB Roles", "Sales Manager", "System Administrator"]
    roles.forEach(x => {
        if (x.name === "Basic User" || x.name === "Sales Manager") {
            hasRole = true;
            return;
        }
    });
    if (hasRole === true) {
        formContext.getControl("jobtitle").setVisible(true);
    } else {
        formContext.getControl("jobtitle").setVisible(false);
    }
}

No comments:

Post a Comment

Hide And Show The Dynamics 365 CRM Button Based On Logged In User Security Roles

 In this post I am going to explain how you will hide and show the Ribbon/Command buttons based on logged in user security rule. I have one ...