Skip to main content

How to Check If a User Is in a Security Group in Power Automate

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

A flow often needs to know who it's working for: a payroll officer sees every department's figures, an approver sees only their own. When the roles are Microsoft Entra security groups, the flow can list a group's members and look for the person among them. Three actions give a plain true or false.

The example is Get Overtime Report, a flow of the Overtime Timesheet app. It checks whether the person who ran it is in the Overtime Payroll group, and returns every department's overtime if they are.

Prerequisites​

  • The group's Object ID.

Step 1: Get the Group's ID​

The example reads the ID from the Settings list with Get items, named Get the payroll group setting: Filter Query Title eq 'PayrollGroupId' and Top Count 1 (How to Filter SharePoint Items with a Filter Query in Power Automate):

The Get the payroll group setting pane: Site Address and List Name set from environment variables, Advanced parameters showing 2 of 6, Filter Query Title eq 'PayrollGroupId', Top Count 1, and Connected to Overtime Timesheet SharePoint

If you'd rather type the ID into the next action, skip this step.

Step 2: List the Group's Members​

Add List group members from the Office 365 Groups connector. I named it List the payroll members.

  • Group Id: the group's Object ID. Here, an expression that takes the Value of the first setting found (How to Get the First Item of an Array in Power Automate):

    List the payroll members → Group Id
    first(body('Get_the_payroll_group_setting')?['value'])?['Value']
  • Top, under Advanced parameters: 999. Microsoft's Office 365 Groups connector reference says it takes from 1 to 999, default is 100. Without it, a group with more than 100 members could leave the person out.

The List the payroll members pane: Group Id set to a first(...) expression, Advanced parameters showing 1 of 1, Top 999, and Connected to Office 365 Groups

The action returns the members in value, each with an id: their Object ID, the same kind of ID Get my profile (V2) returns.

Step 3: Keep the Member Who Is the Person​

Add Filter array (How to Filter an Array in Power Automate), named Caller in payroll:

  • From: value of List the payroll members.
  • The condition: the member's id is equal to the Id of Get the caller.

The Caller in payroll pane: From set to value from Office 365 Groups, and the Filter Query row with the token id, is equal to, and the token Id from Office 365 Users

In Code view, the condition reads @equals(item()?['id'], outputs('Get_the_caller')?['body/id']). The output holds one member if the person is in the group, and none if not.

Step 4: Turn the Result into True or False​

Add Compose (How to Use the Compose Action in Power Automate), named Is payroll, with this expression in Inputs (How to Add an Expression in Power Automate):

Is payroll → Inputs
greater(length(body('Caller_in_payroll')), 0)

The Is payroll pane with Inputs set to a greater(...) expression token

Later actions use outputs('Is_payroll'). In the example, the filter that picks the entries the person may see starts with it: payroll sees everything (How to Use the or() Function in Power Automate).

The Result​

When Leo Martin, who is in Overtime Payroll, opened the app's Reports screen, Is payroll was true:

The Outputs section of Is payroll in a run, with the value true

When Daniel Brooks, an approver who isn't in the group, opened it, it was false:

The Outputs section of Is payroll in another run, with the value false

Common Mistakes​

MistakeWhat happensDo this instead
Leaving Top at its defaultOnly the first 100 members are checkedTop 999, or turn on pagination for larger groups
Comparing names instead of IDsTwo people with the same name matchCompare the Object ID, id
Comparing emails without toLower()A capital letter stops a matchCompare IDs, which don't change
Putting the group ID in every flowMoving to another environment means editing each flowKeep it in one setting

Troubleshooting​

SymptomLikely causeFix
Always falseThe group ID is wrong, or the person is in a group nested inside itCheck the ID; the action lists direct members only
The expression won't saveA bracket is missingCount them; Code view shows the saved text
Always false for one person who is in the groupGet the caller returned someone else, for example the flow ownerRun Get my profile (V2) with the person's own connection (How to Choose the Connections Run-Only Users Use in Power Automate)

FAQ​

Does it check groups inside the group?​

No. Microsoft Graph's List group members returns a group's direct members and isn't transitive. Add people to the group itself.

What if the group has more than 999 members?​

The connector reference says to turn on the action's Pagination setting and give a threshold to get more than 1,000.

Who needs access to the group?​

Whoever owns the connection the action runs with. In the example, run-only users run the flow with the maker's Office 365 Groups connection, so they don't need any access of their own.

Conclusion​

To check a person against a security group in a flow, list the group's members with List group members and Top 999, keep the member whose id matches with Filter array, and turn the count into true or false with greater(length(...), 0).

Additional Resources​