web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Hiding Ribbon Buttons for security roles using JavaScript

(0) ShareShare
ReportReport
Posted on by 277

Hello

We are doing mass Hide/ unhide of ribbon buttons using JavaScript. Which in turn is effecting our Form load performance, Onload the we don't see any ribbon buttons and the form would be frozen, after a while we see the ribbon buttons then the form is functional. We got a reply to the Microsoft ticket saying, As we are calling javascript on each buttons visibility, The script is taking too long to check between users roles going back and fro. They recommended to declare variables for all security roles and compare later to reduce the retrieval time.

Below is the code that I'm using right now. 

function showhideoperation() {        
    debugger;
    var button = GetOperationUser();

    return button;
}

function GetOperationUser() {
    debugger;
    var isUserRole = true;

    var roles = Xrm.Page.context.getUserRoles();
    for (var i = 0; i < roles.length; i++) {
        var role = GetOperationRoles(roles[i].replace("{", "").replace("}", ""));
        if (role)
            return false;
    }


    return isUserRole;
}

function GetOperationRoles(userRoleId) {
    debugger;
    var isUserRole = false;

    var zipcodeFetchXML = ['<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">' +
        '<entity name="role">' +
        '<attribute name="name" />' +
        '<attribute name="businessunitid" />' +
        '<attribute name="roleid" />' +
        '<order attribute="name" descending="false" />' +
        '<filter type="and">' +
        '<condition attribute="roleid" operator="eq" value="' + userRoleId + '" />' +
        '</filter>' +
        '</entity>' +
        '</fetch>'].join('');

    var encodedFetchXML = encodeURIComponent(zipcodeFetchXML);
    var dcs_zipcodeid
    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/roles?fetchXml=" + encodedFetchXML, false);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue");
    req.onreadystatechange = function() {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {

                var results = JSON.parse(this.response);
                for (var i = 0; i < results.value.length; i++) {
                    var roles = results.value[i];
                    //Single Line Text                

                    //UniqueIdentifier
                    if (roles['name'].toLowerCase() == "system administrator") {
                        return false;

                    }
                    if (roles['name'].toLowerCase() == "operation 2") {
                        isUserRole = true;
                    }
                }

            }
            else {
                alert(this.statusText);
            }
        }
    };
    req.send();


    return isUserRole;
}


I'm using the code on multiple buttons of different entities. 

I added Entity privilege rule for majority of buttons, but the remaining buttons the only way is to hide using security roles.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

    Please declare the variable in a list, and  using that to compare instead of using web api to retrieve.  

    I also using this method in my environment and using this method save a lots of time.

    8688.7.png

    But you have to consider for multiple business unit because Same role in different business units is different Guid.

    Example : Guid for Sales Agent Role in Business unit 1 =  1 but in Business unit 2 is 2.

    8688.7.png

    [View:https://msdn.microsoft.com/en-us/library/gg334511.aspx#BKMK_getuserroles:750:50]

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans