Blog

SharePoint - How to check if a user belongs to a Microsoft 365/ SharePoint group

If you need to verify Microsoft 365/SharePoint group membership from a canvas app, this post describes how you can perform this task.

Microsoft 365 groups allow teams to work together better by providing a host of collaboration features and mechanisms to group users for access control/security reasons. There are multiple ways to create a Microsoft 365 group, including Outlook, Microsoft Teams, and SharePoint. The benefit of creating a Microsoft 365 group is that it creates an associated SharePoint site collection, Microsoft Planner Plan, and Outlook group calendar and distribution list.

When building canvas apps, there may be a requirement to check whether the user belongs to a specific Microsoft 365 group. A typical use case scenario is to enable or disable functionality or buttons based on group membership. This post describes how we can carry out this task.

Adding a connection to Office 365 Groups

The first step is to add a data source and select the 'Office 365 Groups' connector.


After this step, we can use this connector to perform tasks that include listing, adding, and deleting group members. We can also use this connector to create and delete calendar events.

The full documentation for this connector is here:

How to display a list of Office 365 Groups

To display a list of available groups, we call the ListGroups method like so:

Office365Groups.ListGroups().value


It's possible to filter the records that are returned by passing an OData expression. For example, we can return groups where the displayName equals "Helpdesk group" like so:

Office365Groups.ListGroups({'$filter':"displayName eq 'Helpdesk group'"}).value
Unfortunately, more complex OData expressions do not appear to be supported, such as calling the 'contains' operator.


How to display the members of an Office 365/SharePoint Group

To display the members of a group, we use the ListGroupMemebers method. This requires us to pass the GUID/ ID value of the Office 365 Group.

Office365Groups.ListGroupMembers("0ed855e2-1d78-45d4-a683-a6882165c325").value
To retrieve the members of an Office 365 group by the group name, we can extend our earlier example and retrieve the ID using a call to ListGroups that includes an OData expression. For example, here's the syntax to retrieve the members of the 'Tim Test' group.

With({
group: First(Office365Groups.ListGroups(
{'$filter':"displayName eq 'Tim Test'"}).value
)
},
Office365Groups.ListGroupMembers(group.id).value
)


How to check if a user (or current user) belongs to an Office 365/SharePoint Group

To check if the user "bob@company.com" belongs to the 'Tim Test' group, we use the following syntax:

With({
group: First(Office365Groups.ListGroups(
{'$filter':"displayName eq 'Tims Test'"}).value
)
},
"bob@company.com" in Office365Groups.ListGroupMembers(group.id).value.mail
)

As an extension of the above, if the currently logged-in user belongs to the 'Tim Test' group, we use the following syntax:

With({
group: First(Office365Groups.ListGroups(
{'$filter':"displayName eq 'Tims Test'"}).value
)
},
User().Email in Office365Groups.ListGroupMembers(group.id).value.mail
)

The formulas above return true or false. In the example scenario where we want to disable a button based on group membership, we can set the DisplayMode property of the button like so:

If(With({
group: First(Office365Groups.ListGroups(
{'$filter':"displayName eq 'Tims Test'"}).value
)
},
User().Email in Office365Groups.ListGroupMembers(group.id).value.mail
),
DisplayMode.Edit,
DisplayMode.Disabled
)


Conclusion

We can verify Microsoft 365/SharePoint group membership by using the Office365Groups connector. This post demonstrated the syntax to carry out this task.
Related posts