Microsoft Flow is a cloud-based software that allows automating tasks across multiple applications.
SharePoint REST API exposes an API which gives information about users, lists, doc libs, permissions in SharePoint. This solely depends on the API we are calling from SharePoint. The result obtained can then be manipulated and used for further functionalities.
In the below steps, we will learn how to integrate MS flows with the SharePoint REST API.
Let’s take a scenario
We have a SharePoint List. On entering a specific item to the list, SharePoint searches whether the user who entered the item is an Admin or not. The Admin users are present in a specific group “RequestAdmin”.The flow then sends an email to the user notifying them whether they are admins or not.
First, let’s create a SharePoint group and create a group named Request Admins. Then, add a few members to it.
While creating the group, we need to follow the below step so that people accessing the flow will be able to retrieve the members from the SharePoint group.
Now, let’s start with the flow.
- We choose SharePoint and then the action as “When list item is created”,
Site Name:FinanceTeam
ListName:PowerAppAdmin
- We assign 2 variables “IsAdministrator” to check whether the person is an admin or not, next “UserGroupInfo” to get the details of the users in the specific group. The “initialize variable” expression is used to achieve the purpose in FLOW.
- Then, we call the SharePoint REST API method on SharePoint group to get the details of the users.
The URI parameter should be given as follows.
/_api/web/sitegroups/getByName('RequestAdmin')/Users?$filter=Email eq 'Email Id of the user entering the list item."}]}
Let’s rename this step as “CheckUserGroup” which will be used later.
- The next part would be assigning “UsergrpInfo” variable to the JSON results we obtained in the previous step. We should use a formula in the expression as below.
Body(‘CheckUserGroup’)[‘d’][‘results’]. This expression gives the JSON results of the user information which is in string format.
- Now, we place a condition like, “If the results are not null”(This implies that the current user exists in the admin group or not), we will send an email based on conditions. We select the parameter “condition” from Flow and add the condition as below.
- If the above condition satisfies, it means users are present in the Admin group and we set the “IsAdministrator” Boolean variable to true and send an email.
- If the condition does not satisfy, “IsAdministrator” is set to false and an email is sent informing the user that he/she is not present in the admin group.
Our flow is completed. We need to save the flow and then test it by adding an item to the list. Here is the screenshot of how the flow looks when it runs successfully.
A few notes on debugging the flow
- If the flow fails at any point, we can check the Run History of the flow and also make a note of the error message for rectifying the issue as below,
- After the issue is fixed, we need not enter the new value, we can test with our old values too. This can be done by selecting “Test Flow” in the top right-hand corner. Then choose “Use data from previous runs” and select the data you want the flow to run with.
P.S
There may be some issues in calling the SharePoint API if the URL is not specified properly. It is better to check in the browser first before adding the service to the flow. A list like “PowerAppAdmin” is already created in the site before configuration of the flow.