Power Apps - Check Group Membership For Groups With > 1,000 Members

What

There are occasions when it is useful to check inside a Canvas App whether the logged-in user is a member of a specific Security Group.

This allows you to implement hide/show functionality for areas of the application that you may only want users/administrators to see.

Why

The standard method I've used in the past to implement this 'group membership' check is to call the following Connector/Function:

Office365Groups.ListGroupMembers()

Using this connector, you would first get a list of All the members of a specific security group, then you would do a Filter on that collection to check if the logged in user is one of the Members.

However!  That connector only works when group membership is less than 1,000 people, if your group has more than 1,000 members, then there's a chance the app will come a cropper for some users.

So below we're going to show you how to do the same check by using MS Graph API calls that all users have access to.

How 

Using a Graph API call, the basic steps are:
  1. Get the ID of the Group you wish to confirm membership for
  2. Call the following Graph API to get a list of all Groups that the logged in user is a member of: https://graph.microsoft.com/v1.0/me/transitiveMemberOf/microsoft.graph.group
  3. Check if the ID from step 1 exists in the collection of ID's from Step 2
//CODE TO CHECK FOR LICENSED USERS WHEN THE GROUP HAS MORE THAN 1,000 MEMBERS

//Description: We are using this method, because the 'Office365Groups.ListGroupMembers' only works for groups with less than 1000 members v(https://learn.microsoft.com/en-us/connectors/office365groups/#list-group-members)

//Thank you to Reza Dorrani for the tips on how to call Graph API directly within a PowerApp: https://www.youtube.com/watch?v=ZzWdXiMzA-c

//Get the ID of the group you're checking membership for
Set(var_LicensedUsersGroupID,First(Office365Groups.ListGroups({'$filter': "displayName eq 'PowerAppsPerUserPlanLicensedUsers'"}).value));

//Get a list of all the groups that I am a member of (the Graph API part):
Set(var_MyGroupMemberships,Office365Groups.HttpRequest("https://graph.microsoft.com/v1.0/me/transitiveMemberOf/microsoft.graph.group","GET","").value);

//The previous action returns an UNTYPED OBJECT, which complicates things a little, but because I know the VALUE property is of type 'ARRAY', I am wrapping the response in a Table() Function.  You can check what the object properties being returned are by running tests in Graph Explorer: https://developer.microsoft.com/en-us/graph/graph-explorer
ClearCollect(col_MyGroupMemberships,Table(var_MyGroupMemberships));

//Check if the ID of the group we are checking is in the collection of group ID's we have.  Once again, we need to return the VALUE column and the expected property to search on, which I know is 'id' because I checked in Graph Explorer.
Set(var_IsLicensed,If(IsBlank(LookUp(col_MyGroupMemberships,Value.id=var_LicensedUsersGroupID.id)),false,true))


The Specific Scenario I was solving

The reason I required this recently, was due to a client that was using PAYG licensing to manage access to premium applications.  The PAYG costs were getting out of control, they had spare licenses, but could not accurately determine who needed them until it was too late.  Therefore we needed a user-friendly way to push staff through this app quickly if they had a license, and stop them if they did not.

We implemented a Timer OnStart to check the final variable, and based on that, launch the correct app (which was passed in as a parameter to the License Checker App.





Thank You

Thank you to Reza Dorrani for the tips on how to call Graph API directly within a PowerApp: https://www.youtube.com/watch?v=ZzWdXiMzA-c


Comments

Popular posts from this blog

Export Group Membership From Active Directory Using Power Query

How to Copy/Duplicate a Table and Columns in a Dataverse Environment

Microsoft Flow - Apply To Each Limitation (5000 items)