Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

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

(0) ShareShare
ReportReport
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
    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
    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
    84,331 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
    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
    84,331 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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
dkrishna Profile Picture

dkrishna 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans