PowerApps - Hide Buttons & Objects based on Office 365 Group Membership

This post shows how you can hide a button inside a PowerApp unless you are a member of a specific O365 Group

Why

A lot of PowerApps we build these days have two audiences:
  • The person filling out a form
  • The team managing the process
The person filling out the form really only needs to do one thing.  Fill out a form, and trust it's been sent to the right people.
The team managing the process may have a review & approval process that they don't want regular staff to see.  They may have Power BI reports or other screens that should be kept private.

I am going to show you how you can hide these buttons & screens so that they are only accessible if you are a member of an Office 365 Group.

How

1. Create Office 365 Group & get ID
First we need an Office 365 Group that we are a member of, and we need to retrieve it's ID.  Here's another post on how to get the ID: How to Quickly find the GUID of a O365 Group

2. We are going to create 2 buttons in a PowerApp, with the intention of hiding one if you are not a member of the O365 Group

3. We are going to add two data source: Office 365 Groups & Office 365 Users
You can do this via 'View Tab > Data Sources > Add Data Source > Office 365 Groups'

4. On the OnStart property of the PowerApp, we are going to add the following code
When you put the following code on the OnStart function of your powerapp, it will:
  • grabs the UPN (profile name) of the logged in user and sets it to a variable 'CurrentUserUPN'
  • Collects a list of all the members of the Office 365 Group into a collection named 'O365GroupMembers'
  • Creates a variable called 'IsO365GroupMember' and sets it to true if the logged in users UPN matches one of the UPN's in the O365GroupMembers collection
OnStart Code:
Set(CurrentUserUPN,Office365Users.MyProfile().UserPrincipalName);
ClearCollect(O365GroupMembers,Office365Groups.ListGroupMembers("[Insert O365 Group ID Here]").value);
Set(IsO365GroupMember,If(!IsBlank(LookUp(O365GroupMembers,userPrincipalName=CurrentUserUPN)),true,false));


5. On the 'Visible' property of the button we want to hide, we are going to insert our IsO365GroupMember Variable into the 'Visible' Property of the Admin Button

And we're done!  Now all you need to do is publish this App and share it with a user who isn't a member of the O365 Group, to see if the button is hidden for them !  Here's me logged in as a test user.

Comments

  1. So I'm not able to get this to work. I used the Graph Explorer (thanks for that tip) to get the ID of the O365 groups that I'm in, and used that in the code. But when I set the visible property to IsO365GroupMember it's coming up as false and not showing my button. Any suggestions?

    ReplyDelete
  2. Disregard my comment above, it's actually working great! I just didn't "Run OnStart" after adding the code to the OnStart property.

    ReplyDelete

Post a Comment

Popular posts from this blog

SharePoint - Field type [FieldName] is not installed properly. Go to the list settings page to delete this field.

Office 365 Groups - Quickly find the GUID of a O365 Group

Export Group Membership From Active Directory Using Power Query