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

Announcements

News and Announcements icon
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

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

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

  • bharrath Profile Picture
    195 on at

    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

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Please, don't create duplicated threads.

  • bharrath Profile Picture
    195 on at

    Hi Andrew,

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

    Thanks,

    Bharath

  • Verified answer
    bharrath Profile Picture
    195 on at

    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

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the June Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
RichardM Profile Picture

RichardM 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans