Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

How to show and hide button bases on logged in security role?

Posted on by 195

Hi friends,

I created one button, "test button". I wanna show button for system customizer role and hide for sales person role. I dont know how to show and hide button based on roles. i have written web api to retrive roles, it is fine but i tried enable roles, still i am able to see test button with sales person role. please help me to achieve this.

Thanks,

Bharath

============================

this is the code i have written, it returns roles successfully

var name;
function roles()
{
var userRoles = Xrm.Page.context.getUserRoles();
for (var i = 0; i < userRoles.length; i++)
{
var userRole = userRoles[i]; // returns the Role Id
alert(userRole);

debugger;
var req = new XMLHttpRequest();
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/roles?$select=name&$filter=roleid eq " + userRole+ "", true);
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 results = JSON.parse(this.response);
for (var i = 0; i < results.value.length; i++) {
name = results.value[i]["name"];
alert(name);
if (name == 'System Administrator')
{
return true;

}
else
{
return false;
}
Xrm.Page.ui.refreshRibbon();


}
} else {
Xrm.Utility.alertDialog(this.statusText);
}
}
};
req.send();
}
}

button_5F00_2.PNGbutton_5F00_3.PNG

button_5F00_1.PNG

*This post is locked for comments

  • Verified answer
    bharrath Profile Picture
    bharrath 195 on at
    RE: How to show and hide button bases on logged in security role?

    Hi friends,

    please find the below code and screen shots to show and hide buttons based on security role.

    var name;

    function roles()

    {

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

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

    {

    var userRole = userRoles[i]; // returns the Role Id

    alert(userRole);

    debugger;

    var req = new XMLHttpRequest();

    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.2/roles?$select=name&$filter=roleid eq " + userRole+ "", true);

    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 results = JSON.parse(this.response);

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

    name = results.value[i]["name"];

    alert(name);

    }

    } else {

    Xrm.Utility.alertDialog(this.statusText);

    }

    }

    };

    req.send();

    }

    }

    function showorhide()

    {

    alert("show or hide");

    if (name == "Salesperson")

    {

    return false;

    }

    else

    {

    return true;

    }

    Xrm.Page.ui.refreshRibbon();

    }

    first create one command and add custom action for that command and use ur webresource and use function name.

    second create enable rule and use custom rule, there u can call js function to show and hide button.

    come back to comand, there u can find display rules and enable rules, there u have to click on add enable rule, u can find already created enable rules, use that rule in the comand, save and publish, login with different roles in different browsers and check.

    1212.Capture4.PNG2474.Capture2.PNG0825.Capture3.PNG

    screenshots

    user with security role "sales person", "test button" is hiding

    1212.Capture4.PNG

    user with "system admin" role, "test button" is showing

    3377.Capture5.PNG

    Hope this helps, If it is helpful hit like.

    Thanks,

    Bharath

  • bharrath Profile Picture
    bharrath 195 on at
    RE: How to show and hide button bases on logged in security role?

    Hi Andrew,

    your suggestion did not solve my problem. can you please help me to solve my problem?

    Thanks,

    Bharath

  • a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: How to show and hide button bases on logged in security role?

    Please, don't create duplicated threads.

  • bharrath Profile Picture
    bharrath 195 on at
    how to hide custom button based on security roles?

    hi friends,

    Can anyone help me to show and hide button based on security roles.

    this is the code i have written. https://community.dynamics.com/crm/f/117/t/302816  

    Thanks,

    Bharath

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: How to show and hide button bases on logged in security role?

    Hello,

    Check my post - butenko.pro/.../showing-ribbon-button-based-on-the-result-of-async-operation

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans