Add Active Directory user to any SharePoint group using Power Automate

To add any user or to share the site with users, we need to go and share the site with the users manually. It is good until and unless we need to share it with a limited number of users.
But it is not possible to share the site and add the users on the fly.
We can add Active Directory users to the SharePoint group.

This blog will guide you how this can be easily done by using Power Automate in integration with SharePoint and Azure Active directory.

Let’s get started

First we need to create a Power Automate (Flow)

Create user workflow using power automate

Add a trigger action item for HTTP request (Post), as we are going to call this Power Automate by using JavaScript/Typescript in a SPFX form.

Add-triger-action

We added this Json Body schema in this post request

{
type“: “object”,
properties“: {
Email“: {
type“: “string”
},
Name“: {
type“: “string”
},
Redirect URL“: {
type“: “string”
},
Message“: {
type“: “string”
},
Group“: {
type“: “string”
}
}
}

Email:  User email
Name:   User name
Redirect URL:   URL to navigate the User to the site, and adds the user in to SharePoint group of this site
Message:   custom message to be send in the invitation email
Group:   SharePoint group name (in which we want to add the external user)

Now need to add the Action item for Azure AD as “Get User”

add-action-item

We will pass the Email into this action item which we are receiving from our Http request action above.
This Action item will check the user with given email in Azure Active Directory. if a user with the same email exists, we can move ahead in flow else flow will give error for no user.

Now after this step I am adding a delay action item, to just make sure the user details received properly before proceeding further. (See in below image)

After delay we are adding Action item “Send an HTTP request to SharePoint”

Send HTTP request

This “HTTP request to SharePoint” can be used to add the User into the SharePoint group, doing this the user get the permission as per the group permission

Site Address :  Site URL which we pass in first action item (Http request) from JavaScript/Typescript
Method :  Post
URI :  _api/web/sitegroups/GetByName(“Group name”)/users
Or
URI :  _api/web/sitegroups(group id)/users (group id = id which SharePoint defines for the individual group)
Header:
 Key:  accept    value: application/json;odata.metadata=none
 Key:  content-type    value: application/json;odata=verbose

Body:

{“__metadata”:{“type”:”SP.User”},
“LoginName”:”i:0#.f|membership|Mail”}

(Mail = email that we get from the output of Get User trigger )

And here we are done, User will be added to the specified SharePoint group.

In addition if you want to send the notification to the newly added user, we can add on one more action item at the end.

send-notification

We can add any custom subject and body text in this action item. This will send the email to the User with the given subject and body

Related Posts

Leave a comment