Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Load JS webresource on EnableRule of a ribbon button

Posted on by 75

Problem:

In Dynamics 365 I have one custom entity say CustomEntity. In that entity's Home Grid Page (List View page, where we get Active/Inactive and so on views) and in entity's form I have added one custom ribbon button, say CustomRibbon. Now, that ribbon button should be visible to users having specific security role, say CSR Admin. How to achieve this ?

What I have tried:

Added custom js (for checking particular Role) in EnableRule for CustomRibbon button. It is working fine for form but not for Grid/List View page of the entity. Reason - js Library is not loading in Grid/List view.
I have also tried callling dummy function for the library and try to call it (so that js webreource can be loaded) on Enable rule, but still it is not working.

*This post is locked for comments

  • Vineet  Mehra Profile Picture
    Vineet Mehra 75 on at
    RE: Load JS webresource on EnableRule of a ribbon button

    Hey Andrii,

    I was also able to solve it today.

    My code is also working, provided I am passing some parameter to the function (although I don't need it, but thats how it is working).

    Thanks for your time and solution!

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Load JS webresource on EnableRule of a ribbon button

    Winnie,

    Here is what how I made it work for me:

    1. Code:

    function isRoleAvailable(roles) {
        var isAvailable = false;
    
        var allRoles = roles.split("|");
    
        var req = new XMLHttpRequest();
        req.open("GET", Xrm.Page.context.getClientUrl() + '/api/data/v8.2/roles?fetchXml=<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true"><entity name="role"><attribute name="name" /><link-entity name="systemuserroles" from="roleid" to="roleid" visible="false" intersect="true"><link-entity name="systemuser" from="systemuserid" to="systemuserid" alias="ac"><filter type="and"><condition attribute="systemuserid" operator="eq-userid" /></filter></link-entity></link-entity></entity></fetch>', false);
        req.setRequestHeader("OData-MaxVersion", "4.0");
        req.setRequestHeader("OData-Version", "4.0");
        req.setRequestHeader("Accept", "application/json");
        req.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
        req.onreadystatechange = function () {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 200) {
                    var results = JSON.parse(this.response);
    
                    var userRoles = results.value;
    
                    for (var i = 0; i < userRoles.length; i++) {
                        for (var j = 0; j < allRoles.length; j++) {
                            if (userRoles[i].name.toLowerCase() === allRoles[j].toLowerCase()) {
                                isAvailable = true;
                                break;
                            }
                        }
    
                        if (isAvailable)
                            break;
                    }
    
                }
            }
        };
        req.send();
    
        return isAvailable;
    }


    2. Usage of that function in custom enable rule:

    EnableRule_5F00_RoleVisibility.png

    If you have several roles that should allow user to see/use button you can pass those roles through separator - | like System Administrator|System Customizer.

  • Vineet  Mehra Profile Picture
    Vineet Mehra 75 on at
    RE: Load JS webresource on EnableRule of a ribbon button

    Thanks Andrii, It will be helpful if you can provide a similar query and how to execute it.

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Load JS webresource on EnableRule of a ribbon button

    Your problem in

    Xrm.Page.context.getUserRoles

    line. It's not available in grids. You will have to build query to get current user instead.

  • Vineet  Mehra Profile Picture
    Vineet Mehra 75 on at
    RE: Load JS webresource on EnableRule of a ribbon button

    I am checking whether the user is having "Field Service - Administrator" role or not.

       fetchUserRoleId: function () {

           debugger;

           var currentUserRoles = Xrm.Page.context.getUserRoles();

           for (var i = 0; i < currentUserRoles.length; i++) {

               var userRoleId = currentUserRoles[i];

               var userRoleName = Winnie.AccountMain.getRoleName(userRoleId);

               if (userRoleName == "Field Service - Administrator") {

                   return true;

               }

           }

           return false;

       },

       getRoleName: function (userRoleId) {

           var name = null;

           var req = new XMLHttpRequest();

           req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/roles(" + userRoleId + ")?$select=name", 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=\"*\"");

           req.onreadystatechange = function () {

               if (this.readyState === 4) {

                   req.onreadystatechange = null;

                   if (this.status === 200) {

                       var result = JSON.parse(this.response);

                       name = result["name"];

                   } else {

                       Xrm.Utility.alertDialog(this.statusText);

                   }

               }

           };

           req.send();

           return name;

       }

  • a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Load JS webresource on EnableRule of a ribbon button

    Winnie,

    Please post your code here.

  • Vineet  Mehra Profile Picture
    Vineet Mehra 75 on at
    RE: Load JS webresource on EnableRule of a ribbon button

    Andrii, the snapshot is having all the components I am using. I have added CustomRule option of EnableRule for Funds button. Right now, I am not worried about Action of that button.

    If possible for you can u please try implementing this scenario.

    It will not take much time. If u want I will share the js code also.

  • a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Load JS webresource on EnableRule of a ribbon button

    Please provide a screenshot of Enable Action in which you load required library.

  • Vineet  Mehra Profile Picture
    Vineet Mehra 75 on at
    RE: Load JS webresource on EnableRule of a ribbon button

    Thanks Andrii, Gopalan

    Ribbon-Workbench.png

    I have used isNaN or dummy functions earlier also for Actions and they worked fine.

    In above snapshot, I have added 2 Funds button one on HomePageGrid and other on the form and added the same enable rule for both button commands. For simplifying it I am just calling a function of AccountMain Library which is checking the role of a user and returning true or false. I am not referencing any other library.

    For Form it is working as expected but not HomePageGrid.

  • Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 on at
    RE: Load JS webresource on EnableRule of a ribbon button

    Hi

    If you are using multiple JavaScript (library) files, along with the JS file in which you are having the function to be used.

    1. Add those library files to webresources

    2. For Actions, add the library files with the function name

    NaN

    3. For Enable rule add the library files with a function which would return true,

    for example:

    function General_IncludeInRibbon()
    {
       return true;
    }

    or
    isNaN
    as mentioned by Andrii

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans