SharePoint Online REST APIs (Part V): SharePoint Groups

Share the love

In the SharePoint Online REST APIs series, I’ll be sharing the most common APIs I use. I mainly use these APIs in Power Automate, so I’ll base the information in this series on the data you need for a Send an HTTP request to SharePoint action.

This article in the series explores how to interact and retrieve data about SharePoint Online groups. This is not a comprehensive list; rather a list of calls that I use when I can’t use predefined Power Automate actions. I have used the color red to identify interchangeable values.

Get a list of SharePoint groups on site

Method

URI

GET

_api/web/SiteGroups

Use this call to get all the SharePoint groups on a site.

Return the users of a specific SharePoint group

Filter by group ID

Method

URI

GET

_api/web/SiteGroups/GetById(3)/Users

If you know the ID of the group on the site, you can return all the users in that group. To find the ID, go to Site Permissions on your site and click into the SharePoint group. In the URL, you’ll see the parameter MembershipGroupId. The number that follows is the ID of this group on this site collection.

Filter by group name

Method

URI

GET

_api/web/SiteGroups/GetByName('Group Name')/Users

If you don’t know the ID of the group, but you know the name, you can also filter by the group name as per the example above.

Check if a user is in a SharePoint group

Method

URI

GET

_api/web/SiteGroups/GetById(3)/Users?$filter=Email eq 'UserEmail@email.com'

We can add onto the above call and check to see if a user is in a particular SharePoint group (for example, you might want to know if the current user is in the Owners SharePoint group). In this call, we filter the results by using Email. Of course, you don’t have to use Email as the filter on this call. You can filter on other user properties. You will also need the group ID which you can find manually, or use the previous call mentioned in this article to get information about the groups (in the example above I have used 3). To find the ID manually, go to Site Permissions on your site and click into the SharePoint group. In the URL, you’ll see the parameter MembershipGroupId. The number that follows is the ID of this group on this site collection.

Find all SharePoint groups that have a similar name

Method

URI

GET

_api/web/SiteGroups/?$filter=substringof('Owner',Title)

Perhaps you have multiple owner or members groups. You can search for all the groups that end with a specific string. In the above call, I’m returning all SharePoint groups that have Owner in the Title.

Add a user to a SharePoint group

Method

URI

Body

POST

_api/Web/SiteGroups(5)/Users

{
"LoginName":"i:0#.f|membership|UserEmail@email.com"
}

Adding a user to a SharePoint group takes a little more work. We have the group ID, but we need to add some information to the Body of the call. We need to find the User Principal Name (or UPN) of the user. An easy way to do this is to write out the first part of the UPN (i:0#.f|membership|), then attach the users email at the end. In some tenants, the UPN and users email may not be the same. If this is the case, you’ll have to provide the whole UPN (which you can receive using the Office 365 connector in Power Automate).

Remove a user from a SharePoint group

Method

URI

POST

_api/Web/SiteGroups(5)/Users/RemoveByID(9)

Finally, we look at removing a user from a SharePoint Group. To make this call, you’ll need the users ID. To find the users ID, you can first use the Check if a user is in a SharePoint group mentioned earlier in this article and find the user ID from the output.


Share the love

3 Replies to “SharePoint Online REST APIs (Part V): SharePoint Groups”

  1. Thank’s for sharing the above mention API it help’s me alot however I am looking for the API from where we can fetch all the groups for which the user is part of, Currently in the above API I am getting the all the groups, however I want to apply the filter or if got the API that can help to find all the groups of the user.

Leave a Reply

Your email address will not be published. Required fields are marked *