Office 365 SharePoint

Perform actions on users in a SharePoint Group with Microsoft Flow using the API

27 augustus 2018

Recently, I was asked to create a Flow for a customer which had some approvals, notifications and some other stuff in it. One of the scenario’s I had to implement was to send a notification to a group of users. First thing that came to mind was to create a SharePoint Group and use the Send an Email action in Flow to send an e-mail to every user in that Group (like we did before in SharePoint Designer workflows).
Unfortunately, Microsoft Flow (currently?!) does not have to possibility to send an e-mail to every user inside a SharePoint Group so I had to came up with another idea.

The Solution

Microsoft recently launched a new Flow action called ‘Send an HTTP request to SharePoint’. With this action you can call the SharePoint REST API and perform all sorts of operations. With this API, you can get all users in a specific SharePoint Group with the following REST call:

https://<siteurl>/_api/web/sitegroups/getbyname(‘<groupname>‘)/users

To get all e-mail addresses from all users within a specific SharePoint Group with Microsoft Flow, you must configure your Flow as follows (I configured this in a custom action Flow, but you can use this in any other flow as well – e.g. a Flow that runs on create or on modified):
First, you want to make the REST call to SharePoint, so you add the Send an HTTP request to SharePoint action and configure it so that you call the API as stated above.


No need to fill in headers and body and the use of complex dictionaries like we had to in SharePoint Designer REST calls!

Next, we must write all results into an array variable by using the ‘Initialize variable’ action. Make sure you select the Array type. The value we are looking for is the outcome of the HTTP request which is the Dynamic content attribute of the Send an HTTP request to SharePoint action called ‘Body’.

Please note that Body will not always appear in the Dynamic content tab. To make sure it does, you can switch to the Expression tab and type something random (like a dot). When you switch back to the Dynamic content tab (no need to click on OK inside the Expression tab), Body will be available. Make sure you remove the random entry before selecting the Body.

We need to make a slight adjustment to the Body to get the results into the variable. That adjustment is to trim the body to only get the results. To do this, simple add [‘d’][‘results’] to the end of Body. The value should look like this:

body(‘Send_an_HTTP_request_to_SharePoint’)[‘d’][‘results’].

Next, we must iterate to every entry inside the array to get specific user information (in this case the e-mail address of the user). Before we start the ‘Apply to each’ loop to iterate through the array, we must initialize two variables:

Name Type Value Used for
i Integer 0 Get values from array
user String Setting the e-mail addresses for every user found

The Apply to each gets its input from the array variable we created before.

To make sure we add every property to the user variable, we need to add the ‘Append to string variable’ action (instead of the ‘Set variable’ action, which only sets the current iteration property and overwrites the previous property) and append the Email property of the user from the current iteration into the variable ‘user’, followed by a semicolon to make sure both properties are separated from each other.

If you are familiar with arrays, you know that an array iteration starts at 0 (that’s why we need the ‘i’ variable and set its value to 0). The next iteration will be 1, etc.

We need to build the value of the Append to string variable action ourselves, because we have to append the property and the semicolon by using the concat() expression.

To do this, go to the Expression tab and select the concat function under string functions. The function will be added to your Expression builder (your cursor will be put inside the concat function. Do not change this!).
Next, we must get the Email property from the user in the current iteration and place it into the first part of the user string variable. You can do this by going to the Dynamic content tab, click the users array variable and then add the starting square bracket (‘[‘). The closing square bracket will automatically be added and your cursor will stay within the square brackets so you don’t need to type the closing bracket. Inside the square brackets, add the i variable.
We now have the entry of the current iteration and need to get the e-mail address. To do that, we need to enter another starting bracket and type ‘Email’ within the brackets.

We now have built the first part of the string, which is the Email property of the user in the current iteration. To add the second part of the string, which is the semicolon, simply add a comma (which will take you to the second part of the user string variable), followed by ‘;’ (including the apostrophes) and make sure the concat function is closed properly with a bracket (this bracket is added automatically by selecting the concat function, but it’s best to check whether it’s still there after all these modifications).

Your expression should look like this:

concat(variables(‘users’)[variables(‘i’)][‘Email’],’;’)

Now you can use this variable inside a ‘Send an email’ action or an approval action. You can get other properties as well (e.g. DisplayName) by changing the [‘Email’] suffix to any other known user property.
To make sure we get the e-mail address from each iteration and not only from the first (i=0, as we configured it by initializing the variable), we have to end our Apply to each loop by incrementing our i variable at the end of the Apply to each loop by using the Increment variable action:

That’s it, you can now perform actions (e.g. send an email) on users in a SharePoint Group!

Source: https://www.about365.nl/2018/06/05/microsoft-flow-perform-actions-on-users-in-a-sharepoint-group-using-the-api/

You Might Also Like

Geen reacties

Plaats een reactie

Deze website gebruikt Akismet om spam te verminderen. Bekijk hoe je reactie-gegevens worden verwerkt.