Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Hide Tab Based on Security Role

Posted on by Microsoft Employee

Hi

I want to hide a tab on a form if the user does not have a certain security role. I am currently using this JS to get all the roles of the current user. How do I then extend this to show/hide the tab? At the moment, I have it as an on load function and it returns a series of alerts showing each role the user has.

function GetUserRoles() {
var roles = Xrm.Page.context.getUserRoles();

for (var i = 0; i < roles.length; i++) {
GetRole(roles[i]);
}
alert(roles);
}

function GetRole(roleid) {
var serverUrl = Xrm.Page.context.getClientUrl();
var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/RoleSet?$select=Name&$filter=RoleId eq guid'" + roleid + "'";

var retrieveReq = new XMLHttpRequest();
retrieveReq.open("GET", oDataSelect, false);
retrieveReq.setRequestHeader("Accept", "application/json");
retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
retrieveReq.onreadystatechange = function () {
GetRoleData(this);
};
retrieveReq.send();
}

function GetRoleData(retrieveReq) {
if (retrieveReq.readyState == 4) {
if (retrieveReq.status == 200) {
var retrieved = JSON.parse(retrieveReq.responseText).d;
alert(retrieved.results[0].Name);
}
}
}

Thanks in advance.

*This post is locked for comments

  • naZir Profile Picture
    naZir 850 on at
    RE: Hide Tab Based on Security Role

    function GetUserRoles() {
    debugger;
    var CheckRole = false;
    var roleid = Xrm.Page.context.getUserRoles();
    for (var i = 0; i < roleid.length; i++) {
    var roleID = roleid[i];
    var RoleName = GetRole(roleID);

    if (RoleName == "Sales Manager") {
    var CheckRole = true;
    }
    }
    if (CheckRole == true) {
    //Put Your code here what you want to do for some specific role
    }
    else {

    //here write the else part
    }
    }


    function GetRole(roleid) {
    debugger;
    var serverUrl = Xrm.Page.context.getClientUrl();
    var oDataSelect = serverUrl + "/XRMServices/2011/OrganizationData.svc/RoleSet?$select=Name&$filter=RoleId eq guid'" + roleid + "'";
    var name = null;
    var retrieveReq = new XMLHttpRequest();
    retrieveReq.open("GET", oDataSelect, false);
    retrieveReq.setRequestHeader("Accept", "application/json");
    retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");
    retrieveReq.onreadystatechange = function () {
    name = GetRoleData(this);
    };
    retrieveReq.send();
    return name;
    }

    function GetRoleData(retrieveReq) {
    debugger;
    var roleName = null;
    if (retrieveReq.readyState == 4) {
    if (retrieveReq.status == 200) {
    var retrieved = JSON.parse(retrieveReq.responseText).d;
    roleName = retrieved.results[0].Name;
    return roleName;
    }
    }
    }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Hide Tab Based on Security Role

    Thanks everyone for your help.

  • Verified answer
    Aileen Gusni Profile Picture
    Aileen Gusni 44,522 on at
    RE: Hide Tab Based on Security Role

    Hi Vikas,

    The context.GetUserRoles() will only result to GUID, not the Name,

    If you don't want to use Guid, you can get the name by querying to the CRM Data using the GUID you obtained as criteria

    neilparkhurst.com/.../javascript-return-roles

    social.microsoft.com/.../get-security-role-name-using-javascript

    Hope this helps.

    Thank you.

  • EmployeeOcta Profile Picture
    EmployeeOcta on at
    RE: Hide Tab Based on Security Role

    Vikas ,

    I believe the if condition you have written will not work.

    because you are writing roles[i].Name  but Name property is not there.

    how  you are wrting that.

    Hello Rich,

    write if condition for checking guids of the roles.

    if (roles[i] == "Guid of the role")  

    and then check it will work.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Hide Tab Based on Security Role

    add debugger in the code like this

    function GetUserRoles() {

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

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

        //GetRole(roles[i]);

    debugger;

    if (roles[i].Name == "Security Role A")

    {

    alert("found");

    Xrm.Page.ui.tabs.get("tab_results").setVisible(true);

    }

    else

    {

    alert("no");

    Xrm.Page.ui.tabs.get("tab_results").setVisible(false);

    }

    }

    }

    Open developer console (press f12). Refresh the page then check in the console for each item in loop, you would be able to check the problem. I would again say that match GUID instead of name.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Hide Tab Based on Security Role

    As per Vikas' suggestion, I've now using this:

    function GetUserRoles() {

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

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

         GetRole(roles[i]);

    if (roles[i].Name == "Security Role A")

    {

    alert("found");

    Xrm.Page.ui.tabs.get("tab_results").setVisible(true);

    }

    else

    {

    alert("no");

    Xrm.Page.ui.tabs.get("tab_results").setVisible(false);

    }

     }

    }

    I put the alerts in to see if it works correctly, but even though the user does have the security role, it returns 'no' each cycle of the for loop...

    Any ideas?

  • EmployeeOcta Profile Picture
    EmployeeOcta on at
    RE: Hide Tab Based on Security Role

    In the debugger console you can write below lines of code for testing .

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

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

    console.log(roles[i]);

    }

    by clicking f12 key on the webpage.

    and in the debugger write above code and it will write the guids within the console.

  • EmployeeOcta Profile Picture
    EmployeeOcta on at
    RE: Hide Tab Based on Security Role

    in the if condition you are directly writing the roles and which the array of object..

    iterate the individual role[i]. but Xrm.Page.context.getUserRoles(); will give you the array of Guid's and not name.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Hide Tab Based on Security Role

    You should match the name inside the loop. Something like

    function GetUserRoles() {

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

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

          GetRole(roles[i]);

    if (roles[i].Name == "Security Role A")

    {

    Xrm.Page.ui.tabs.get("tab_results").setVisible(true);

    }

    else

    {

    Xrm.Page.ui.tabs.get("tab_results").setVisible(false);

    }

      }

    }

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Hide Tab Based on Security Role

    I tried this, but it doesn't work:

    function GetUserRoles() {

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

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

           GetRole(roles[i]);

       }

    alert(roles);

    if (roles == "Security Role A")

    {

    Xrm.Page.ui.tabs.get("tab_results").setVisible(true);

    }

    else

    {

    Xrm.Page.ui.tabs.get("tab_results").setVisible(false);

    }

    }

    How do I say if the array contains?

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,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans