Skip to main content

Notifications

Announcements

No record found.

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

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    Alex Fun Wei Jie 33,626 on at
    RE: Hiding Ribbon Buttons for security roles using JavaScript

    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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,403 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans