Articles

How to add users to SharePoint group using Flow

How to add users to SharePoint group using Flow

In this article we will see how to add users to SharePoint group using Microsoft Flow or Microsoft Power Automate.

Let's start by having a look on the rest api to add users to SharePoint group

 

Rest api to add users to SharePoint group

Rest end point to add users into SharePoint group is given below


_api/web/sitegroups(groupid)/users

So to add users to group we need to make POST request to this endpoint and of course we need to pass user as metadata as below


{"__metadata":{"type":"SP.User"},"LoginName":"abc"}  

 Now to we know the main task, lets start creating the flow.

Create a mannually triggered Flow

In order to create a mannually triggered flow, follow below steps.

  1. Go to https://flow.microsoft.com

  2. Click on My flows in left section

  3. Click new top and select Instant - from blank

  4. Choose mannually triggred flow and click Create

At this time your flow will be created. Now lets say we know the group name and then we need to have group id in order to add user to that group

Get Group Id by Group name

Add a new step and select "Send an HTTP request to SharePoint"

1. Enter your site address 

2. Choose method as GET

3. In URI inter below


_api/web/SiteGroups?$filter=LoginName eq 'group name'&$select=Id&$top=1 

  Replace group name with your group here and add two headers as below 


accept : application/json;odata=verbose

content-type : application/json;odata=verbose

we will need to parse the response to get group Id. run the the flow once and from run history copy the response body of above activity.

Getting Group Id 

To get group id, We can parse the body of reponse of request we send to get the group above. but to parse the response we need to have schema and luckly we have a way to get schema.

Just run the flow once and then go to run history and click on send request to SharePoint step and it will be expanded and then copy the json body of the response and have it some where.
 
Now return back to flow, and add a new step and choose parse json.
In body select body from send an HTTP request to SharePoint and in schema
Click generate from sample and paste the json copied in previous step and you are done.
 

Add users to SharePoint group

Now we know the group id and we need to add users to SharePoint group. Lets add a new step "Send an HTTP request to SharePoint" and choose below

  1. In Site address, enter your site address
  2. Choose POST method
  3. In URI enter below                                                                                                                  
    
    _api/web/sitegroups("select id from above parse json step")/users
    
    notice that your activity will be enclosed with apply to each step loop.This is because result obtained from parse Json step is collection.
  4. Add two headers accept and content-type the value of both will be application/json;odata=verbose
  5. Enter metadata as below                                                                                                                                                                                      
    {"__metadata":{"type":"SP.User"},"LoginName":"abc"}  
    
    add your user name to add on the place of abc and you are done. test the flow by mannually triggering and see the result

SharePoint Online
Microsoft Flow

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Mukesh Tiwari
Always ready to learn new technologies, A problem solver, exhibit to do attitude. Having around 10+ years of experience of development and management skills. I believe don't just set random goals but right goals suited for you.
Page Views :    Published Date : Jun 27,2020  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!