Skip to main content

Notifications

Power Apps - Building Power Apps
Answered

Hide tab based on users security role

Posted on 25 Nov 2024 20:17:10 by 6
Hi, 
 
I have a model driven power app in which I need to hide a certain tab if the user has a certain security role. I followed the instructions here to do this via javascript.
 
I now have the problem that the tab is always hidden, regardless of the users security role. My javascript is;
 
function onLoad(execCtx) {
    const formCtx = execCtx.getFormContext();
    
    const roleName = "Project User";
    const isUser = Xrm.Utility.getGlobalContext().userSettings.roles.get(r => r.name == roleName);

    if (isUser) {
        formCtx.ui.tabs.get("tab_6").setVisible(false);
    }
}
 
Can anyone help me with this?
Categories:
  • jbyrne1 Profile Picture
    jbyrne1 6 on 26 Nov 2024 at 08:53:01
    Hide tab based on users security role
    That worked! Thank you so much SaiRT14.
  • Verified answer
    SaiRT14 Profile Picture
    SaiRT14 1,216 on 25 Nov 2024 at 21:51:37
    Hide tab based on users security role
    pls try the following:
     
    function onLoad(execCtx) {
        const formCtx = execCtx.getFormContext();
        const globalContext = Xrm.Utility.getGlobalContext();
        const roleName = "Project User"; // Replace with your exact role name
        const userRoles = globalContext.userSettings.roles;
        let hasRole = false;
        // Iterate through the user's roles to check for the specified role
        userRoles.forEach(function (role) {
            if (role.name === roleName) {
                hasRole = true;
            }
        });
        // Hide the tab if the user has the specified role
        if (hasRole) {
            formCtx.ui.tabs.get("tab_6").setVisible(false);
        }
    }
     
    it should fix your issue
     

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

November 2024 Newsletter…

November 2024 Community Newsletter…

Community Update Oct 28…

Power Platform Community Update…

Tuesday Tip #7 Community Profile Tips…

Welcome to a brand new series, Tuesday Tips…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 143,246

#2
RandyHayes Profile Picture

RandyHayes 76,308

#3
Pstork1 Profile Picture

Pstork1 63,884

Leaderboard