Add or Remove users from SharePoint groups using Power Automate
Hello there! Today we’re going to learn how to add or remove users in SharePoint groups using Power Automate flows. This will be one part of many upcoming posts on SharePoint Custom Permission management using Power Platform.
Let’s have a look at the case scenario!
We have many document libraries and folders in our SharePoint sites, and based on the roles of different users accessing the libraries or folders, we have created different SharePoint groups. Now when someone wants to get access to a particular library or folder then the admin has to lookup the SharePoint group related to those assets and add users to that group. Our task now is to build a flow to manage this
For this case scenario, I want to add/remove users from a SharePoint group through a Canvas app, as we will be continuing the same in upcoming posts.
1. To start off, we’ll first need to know the ID of our groups. You can go to the group’s page of your site /sites/#SiteName/_layouts/15/groups.aspx
2. Now let’s store all the group IDs in a list, as we will need this information later
3. Let’s now jump into the flow! I’ll be using a PowerApp trigger, as I’m using a Canvas app as the UI for this. We will take the Emails of users to add/remove, the boolean value for checking what action to make, and then the Group ID to know SharePoint group the users needs to be added/removed
4. Next I’ll use an array variable to split our emails from the trigger – which are a string of emails with a delimiter
split(triggerBody()[‘text’],’,’)
5. In the next step, we’ll add an “Apply to each” to iterate through the emails, and then a condition to check if we need to add or remove the user, using the “Add” boolean input
6. Now for the Yes branch, we need to add the user to the selected group,
7. For the No branch, we need to remove the user from the selected Group
8. We now have the flow setup! Now inside our Power App, we need to take the inputs, which are the user details and the group to be added/removed from. We will add a multi-choice people picker field for selecting the users, a combo box for selecting the group, and then the “Add” or “Remove” buttons for triggering the flow.
9. Next we need to add the flow in our App and trigger it using the buttons. For that, we’ll first need to add our Flow to the App. You can do it from the left panel by selecting the Power Automate icon from left menu->Add Flow-> Select the flow
10. Finally, using the flow we just added, we will write the Formula to trigger it in the “onSelect” property of the buttons, and then pass the parameters.
Formula for Add button:
‘AddorRemoveuserfromSharePointgroup’.Run(Left(Concat(dd_users.SelectedItems, Claims & “,”),Len(Concat(dd_users.SelectedItems, Claims & “,”)) -1),true,dd_group.Selected.GroupID, dd_group.Selected.Site)
Formula for Remove button:
‘AddorRemoveuserfromSharePointgroup’.Run(Left(Concat(dd_users.SelectedItems, Email & “,”),Len(Concat(dd_users.SelectedItems, Email & “,”)) -1),false,dd_group.Selected.GroupID, dd_group.Selected.Site)
Let’s check out how it works!! In the below gif, I’m first checking the existing users in a group and adding/removing users to that group using the flow we built
5 Comments
Guna
Sir, if I manually ran the flow it was working add\Remove. But if i go with app. this formual is showing error
Guna
Hi Sir\Madam,
iterlly am not understanding this formual. Same order i was created Sharepoint List, Power Apps name along with variable, then why still this formual shows error.
‘20220302-AddusertoSPGroup’.Run(‘AddorRemoveuserfromSharePointgroup’.Run(Left(Concat(dd_users.SelectedItems, Claims & “,”),Len(Concat(dd_users.SelectedItems, Claims & “,”)) -1),true,dd_group.Selected.GroupID, dd_group.Selected.Site)
am not understan what is AddorRemoveUserfromsharepointgroup.. this sharepointgroup what is this?
Guna
Hi Sir, Kindly advice this formula
what is this formula, this same formula i copy and paste,. but not working. my colleague said, Don’t copy and paste it anything, try to understand function. but i don’t know how to write this function
‘AddorRemoveuserfromSharePointgroup’.Run(Left(Concat(dd_users.SelectedItems, Claims & “,”),Len(Concat(dd_users.SelectedItems, Claims & “,”)) -1),true,dd_group.Selected.GroupID, dd_group.Selected.Site)
shaiksha35
Hi Guna,
The formula is for triggering the flow that we just created from the Power App. I’ve updated the post (Steps 8-10) to include the way to add the Power Automate that we just created into the app and then use it.
Guna
Sir, you are really great and wonderful work on this one… Superb…