Create SharePoint Groups And Add Users To A SharePoint Online Site Or Subsite Using Power Automate Flow

In this article, we will see how we can create a flow in Power Automate to create SharePoint groups and add users to a SharePoint site or subsite. In our scenario, we have a SharePoint online list at root site collection level. When a new item is added to this list, one SharePoint subsite is created within root site collection. This list has all the details like subsite Title, Description, Owners, Members, etc as shown in the below screenshot. Our flow will take the Title of subsite and create 2 SharePoint groups. One for subsite owners and other for subsite members. In these 2 new SharePoint groups for subsite, flow will look for the users in Owners field of this list and add it to Owners group. Similarly, flow will get all the users in Members field of the list and add all users to newly created Members group.

Using Power Automate flow to create SharePoint groups and add users to a SharePoint online site or subsite

Trigger

This flow is triggered when an item is created or updated in the list of subsites at root site collection level.

Using Power Automate flow to create SharePoint groups and add users to a SharePoint online site or subsite

Initialize variables

Initialize following variables to be used in flow logic.

Using Power Automate flow to create SharePoint groups and add users to a SharePoint online site or subsite

Site Title: This will have title of the subsite

Site Url: This will have URL of the subsite

Owners Group Name

This will have owners group name. Use following expression

(concat(variables('varSiteTitle'),' ','Owners')

Owners Group ID

To store ID of the owners group.

Members Group Name

This will have members group name. Use the following expression

(concat(variables('varSiteTitle'),' ','Members')

Members Group ID

To store ID of the members group.

Flow logic

First, we will create Owners group and add users to this group, then we will create members group and add users to this group. Last, we will add these two groups to subsite.

Using Power Automate flow to create SharePoint groups and add users to a SharePoint online site or subsite

Create Owners Group and add users

Using Power Automate flow to create SharePoint groups and add users to a SharePoint online site or subsite

Use 'Send an HTTP request to SharePoint' GET method to get Group ID by passing group name.

Using Power Automate flow to create SharePoint groups and add users to a SharePoint online site or subsite

Uri

_api/web/SiteGroups?$filter=LoginName eq '@{variables('varOwnersGroupName')}'&$select=Id&$top=1

Headers

{
    "accept": "application/json;odata=verbose",
    "content-type": "application/json;odata=verbose"
}

Use 'Send an HTTP request to SharePoint' GET method to get all groups

Using Power Automate flow to create SharePoint groups and add users to a SharePoint online site or subsite

Condition

Check if Group Exist: If Group ID is not 0 then group already exists.

Using Power Automate flow to create SharePoint groups and add users to a SharePoint online site or subsite

Expression

length(outputs('Send_an_HTTP_request_to_SharePoint-_Get_Group_ID')?['body/d/results'])

Create Group if it does not exist and add users to this group

Using Power Automate flow to create SharePoint groups and add users to a SharePoint online site or subsite

Create Group

Using Power Automate flow to create SharePoint groups and add users to a SharePoint online site or subsite

Method: POST

Uri: _api/web/SiteGroups

Headers

{
    "accept": "application/json;odata=verbose",
    "content-type": "application/json;odata=verbose"
}

Body

{
    "__metadata": {
        "type": "SP.Group"
    },
    "Title": "@{variables('varOwnersGroupName')}",
    "Description": "@{triggerOutputs()?['body/Title']} Owners Group"
}

Compose-GroupID

Using Power Automate flow to create SharePoint groups and add users to a SharePoint online site or subsite

Input

@{outputs('Send_an_HTTP_request_to_SharePoint_-_Create_Group')?['body']?['d']?['id']}

Set variable -varOwnersGroupID

Using Power Automate flow to create SharePoint groups and add users to a SharePoint online site or subsite

@{outputs('Compose-GroupID')}

Add all users from Owners field to this Owners group.

Using Power Automate flow to create SharePoint groups and add users to a SharePoint online site or subsite

Method: POST

Uri: _api/web/SiteGroups(@{variables('varOwnersGroupID')})/users

Headers

{
    "accept": "application/json;odata=verbose",
    "content-type": "application/json;odata=verbose"
}

Body

{
    "__metadata": {
        "type": "SP.User"
    },
    "LoginName": "@{items('Apply_to_each_Owners')?['Claims']}"
}

If SharePoint Owners group already exist then you need to get group id and add all the owners from SharePoint list field into it.

Using Power Automate flow to create SharePoint groups and add users to a SharePoint online site or subsite

Filter array

Using Power Automate flow to create SharePoint groups and add users to a SharePoint online site or subsite

From

@body('Send_an_HTTP_request_to_SharePoint-_Get_Groups')?['body']?['d']?['results']

From array of all site groups, we are filtering the group which has same Title as Owners group name

Compose- Group ID

Using Power Automate flow to create SharePoint groups and add users to a SharePoint online site or subsite

Expression: body('Filter_array')?[0]['Id']

Set variable -varOwnersGroupID2

Using Power Automate flow to create SharePoint groups and add users to a SharePoint online site or subsite

Adding users to this group is same as explained above in this article.

Create Members Group and assign users

This is exactly same as creating owners’ group and adding users to it.

Using Power Automate flow to create SharePoint groups and add users to a SharePoint online site or subsite

Add groups to subsite

Now we have both groups created and users added to both groups, we have to add these 2 groups to subsite.

Using Power Automate flow to create SharePoint groups and add users to a SharePoint online site or subsite

Add Owners Group

Using Power Automate flow to create SharePoint groups and add users to a SharePoint online site or subsite

Send an HTTP request to SharePoint- Add Owners Group – Full Control

Method: POST

Uri: _api/web/roleassignments/addroleassignment(principalid=@{variables('varOwnersGroupID')},roledefid=1073741829)

Headers

{
    "accept": "application/json;odata=verbose",
    "content-type": "application/json;odata=verbose"
}

Add Members Group

Using Power Automate flow to create SharePoint groups and add users to a SharePoint online site or subsite

This group will have Edit permissions. All other inputs are same as for Owners.


Similar Articles