Show/Hide Button In Power Apps Based On The Current User

Show/Hide Button In Power Apps Based On The Current User

Need to show or hide a button in Power Apps based on the current user?  There are several ways to do it.  The quickest way is to comparing the current user’s email to a hardcoded value to see if it matches.  But the better way is is to check if the current user belongs to a specific Azure AD group or is found in a SharePoint list.  In this article I will show 3 ways to show/hide a button in Power Apps so you can decide which one is best for you.

Table Of Contents:
Introduction: The Project Tracker App
Option #1: Show/Hide Button If Current User Matches Hardcoded Email
Check The Current Apps User's Email Address
Option #2: Show/Hide Button If Current User Is In Azure AD GroupAdd The Azure AD ConnectorCreate An Azure AD Security GroupCheck The Azure AD Group For Current App User
Option #3: Show/Hide Button If Current User Is In SharePoint ListSetup The App Users SharePoint ListCheck The SharePoint List For Current App User




Introduction: The Project Tracker App

The Project Tracker app is used by Project Managers and other employees at a construction company to manage locations, review works orders and create invoices. When a Project Manager is logged the app’s menu shows a settings button. Pressing this button will navigate the app to a screen that only the Project Manager should see.



When an Employee who is not a Project Manager logs into the app the Settings button becomes hidden. Employees should not have access to the Settings Screen.




Option #1: Show/Hide Button If Current User Matches Hardcoded Email

The simplest way to show or hide a button based on the current user is to compare their email address to an email address hard-coded into the app and see if it matches. When both emails addresses match the button will show and when they do not match the button will be hidden. This is the easiest option to show/hide a button but it has a disadvantages over the other options. If the hardcoded email ever needs to be changed we must edit and republish the app because the email address is hardcoded instead of being retrieved from a datasource.



Check The Current App User’s Email Address

Open Power Apps Studio and create a new app that looks like the one shown below. Include a red Settings button that will show/hide based on the current user.



Write this code in the OnStart property of the app. The User function retrieves the current logged in user’s email and this value gets stored in the variable varUserEmail. Then we check if varUserEmail matches the Project Manager’s email and save the result in the varIsProjectManager variable. A match returns true and a non-match returns false.

Set(varUserEmail, User().Email);
Set(varIsProjectManager, varUserEmail = "md@matthewdevaney.com");



To execute the code in OnStart for testing purposes, click on the three dots beside App and select Run OnStart. varUserEmail and varIsProjectManager are now updated with values.



Now we will control the Settings button’s visibility with the varIsProjectManager variable.



Select the Settings button and type the varIsProjectManager value into the Visible property. When the variable is true the button will show and when it is false the button will hide.

varIsProjectManager




Option #2: Show/Hide Button If Current User Is In Azure AD Security Group

A better way to show or hide a button based on the current user is to check if they belong to a specific security group in Azure Active Directory (Azure AD). Azure AD has a list of all users in a company and organizes them into security groups to control access to files and applications. Every organization with Windows devices uses Azure AD but you may not have seen it before because typically only the I/T department has access.

If we setup a Project Managers group in Azure AD, we can ask the I/T department to give access to every Project Manager who joins the organization as a companywide policy. Then we do not need to edit and republish the app to change who can see the settings button. Our app simply checks the Azure AD group to see if the user is a member.




Add The Azure AD Connector

Add the Azure AD connector to the app from the Add data menu.



Create An Azure AD Security Group

Now we will setup the Project Managers security group in Azure Active Directory. Open Azure AD and go to the Groups area.



Select New group.



Setup the new security group as shown below.



Add Owners and Members. Groups are managed by the Owners. Assign yourself as an Owner. Members are the Project Managers who will be using our app and should access to the red Settings button. Select as many members as you like and then click the Create button to Save the group.



The Project Managers group will now show in the list of All Groups. Copy the Object Id onto the clipboard. We will need it to reference the Azure AD security group in Power Apps.




Check The Azure AD Group For Current User

The Project Managers group is now setup in Azure AD so now we can check if the current user belongs to the group and whether to show or hide button.



Use this code in the OnStart property of the app. The CheckMemberGroupsV2 method of the Azure AD connector checks if the user’s email is found within a list of specified Azure AD groups. The group identifier in this code is the Project Managers group Object Id from Azure AD. If the result is not blank, it means the current user is a Project Manager and should see the button. Or if the function returns blank the user is an other employee we must hide the button.

Set(varUserEmail, User().Email);
Set(
    varIsProjectManager,
    !IsBlank(
        AzureAD.CheckMemberGroupsV2(
            varUserEmail,
            ["e488a52e-1b55-4042-a2c1-680a3b7ab158"]
        )
    )
)



Run App OnStart to refresh the values for varUserEmail and varIsProjectManager. Then use the varIsProjectManager variable in the Visible property of the red Settings button to control whether it should be shown or hidden.

varIsProjectManager




Option #3: Show/Hide Button If Current User Is In SharePoint List

We have one other option to show or hide button for the current user in Power Apps based on a group if we are unable to use Azure Active Directory. Instead we can setup a list of users and roles in a SharePoint list. This places the responsibility for maintaining access to the app outside of the I/T department. Now We can quickly change who has access ourselves and we do not need to wait for I/T. But the downside is we no longer benefit from a strong centerally managed I/T security policy.




Setup The App Users SharePoint List

Create a new SharePoint list called App Users List with the following columns:

  • User – Person type
  • Role – Choice type




The Role column should have two choices: Employee and Project Manager. Once you have defined the roles setup your list of users and give at least one user the role of Project Manager.




Check The SharePoint List For Current App User

Open Power Apps and add the App Users List SharePoint list from the Add data menu.



Write this code in the OnStart property of the app. The LookUp function checks the App Users List to see if any records match the current user’s email and have the role of Project Manager. If the result is not blank, it means the current user is a Project Manager and should see the button. Or if the function returns blank the user is an other employee we must hide the button.

Set(varUserEmail, User().Email);
Set(
    varIsProjectManager,
    !IsBlank(
        LookUp(
            'App Users List',
            And(
                User.Email = varUserEmail,
                Role.Value = "Project Manager"
            )
        )
    )
);



Write the varIsUserProjectManager variable in the Visible property of the red Settings button to control whether it shows or hides for the current user.

varIsUserProjectManager





Questions?

If you have any questions about Show/Hide Button In Power Apps Based On The Current User please leave a message in the comments section below. You can post using your email address and are not required to create an account to join the discussion.

Matthew Devaney

Subscribe
Notify of
guest
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
yuo
yuo
11 days ago

forgot the USER in isUSERprojectmanager text