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.
Trigger
This flow is triggered when an item is created or updated in the list of subsites at root site collection level.
Initialize variables
Initialize following variables to be used in flow logic.
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.
Create Owners Group and add users
Use 'Send an HTTP request to SharePoint' GET method to get Group ID by passing group name.
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
Condition
Check if Group Exist: If Group ID is not 0 then group already exists.
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
Create Group
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
Input
@{outputs('Send_an_HTTP_request_to_SharePoint_-_Create_Group')?['body']?['d']?['id']}
Set variable -varOwnersGroupID
@{outputs('Compose-GroupID')}
Add all users from Owners field to this Owners group.
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.
Filter array
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
Expression: body('Filter_array')?[0]['Id']
Set variable -varOwnersGroupID2
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.
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.
Add Owners Group
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
This group will have Edit permissions. All other inputs are same as for Owners.