Sunday, August 21, 2022

Access SharePoint Groups In Power Automate

Hello Friends,
Today, we will learn about working with SharePoint Groups in Power Automate. Sometimes, we have a requirement in which, we have a SharePoint group in our SharePoint site and we want to allow some activities to the members of those group in PowerApps. As we know that SharePoint Groups are not accessible in PowerApps and also not accessible directly in Power Automate, therefore, we will use some work around to access them in Power Automate flow to achieve this functionality. There are 2 ways to get the SharePoint Group members. If you know the group id, then we can directly use the HTTP request to get the group members. Otherwise, if you know the group name only, first we will get, all the groups, then we will extract the desired group from the response. From this, we will get the Group ID. Then we will use the HTTP request to get the group members using this id. 

 So, let's begin.
  1. We have created a group named "TestDesignEditGroup". I am the member of this group.
  2. Our first objective is to get the SharePoint Group Id for this group.
  3. Let's create a flow. I am talking Instant cloud flow >> Manually trigger a flow and given a name "POC-GetSharePointGroupsMembers".
  4. Now we will define 3 variables-
    1. strGroupName - This variable will hold the name of the group we are looking for
    2. intGroupID - This variable will hold the ID of the group mentioned in strGroupName
    3. strGroupMemberDisplayNames - This variable will hold the semicolon separated display names of the members of the group
  5. Save the workflow.
  6. Now, as we know that Power Automate doesn't have any direct action to connect with SharePoint Groups, we will first use HTTP action to get all the SharePoint Groups.
  7. It will ask few information-
  8. It will give us a JSON having information of all the SharePoint groups.
  9. Now, from there, we have 2 ways to extract the information-
    1. Using "Apply to each"
    2. Using "Filter"
  10. Let's try "Apply to each" first.
  11. Add an action "Apply to each".
  12. As we can see in content window, only "body" option is available. However, we need the "value" part of the output. So, we will click on "Expression" tab and use the dynamic expression.
    1. body('Send_an_HTTP_request_to_SharePoint_-_Groups')?['value']
  13. Add a "Condition" action where we will check if the "Title" of the group is equal to what we are looking for.
  14.  
  15. If, the match is found, set the "Id" of that group to the variable "intGroupID".
  16. Now, we have the ID of the group. We need to again use HTTP request action to get the group members of this group.
  17. Add another HTTP request action.
  18. It will give us the JSON of members of the group.
  19. We need to use another "Apply to each" action upon this JSON to fetch the "Title" (member display name) of each member. We will append this Title alongwith semicolon to the variable "strGroupMemberDisplayNames".
  20. That's all. Now, it's time to test. Save the flow and click on "Test" link provided at top right corner. 
  21. Below are the screenshots of each action (one by one), we have used here.
  22. Similar way, if you wish to extract EMail IDs, then instead of Title attribute, you have to extract the Email attribute.
  23. This way, you can get the members of SharePoint group. In case, if you already know the SharePoint group ID, then after initializing the variables, you may directly jump to the HTTP action "Send an HTTP request to SharePoint - Members".
  24. Now, try the other way using "Filter".
  25. Let's try using "Filter".
  26. In the above flow, delete the "Apply to each - Groups" action and add below 2 actions.
  27. Add Filter action.
  28. Add the filter criteria.
  29. Now add another action called "Set variable". We will not fetch the Id of the group from the output of filter action and assign it to intGroupID. If you want to check if the output of filter action is not null, then you may add a Condition action and inside that condition action, if the output is not null then you may add the set variable action there.
  30. That's all. Rest of the 2 actions (to get the members of the group and then getting their display names) will remain same.
With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.

Stay Safe !
Stay Healthy !

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.