Friday, August 13, 2021

Power Automate: Change User Permission (List Item)

  Hello Friends,

Welcome back with another post on Power Automate. In my last post we were talking about Remove / Restore the permissions inheritance. You may visit below URL to read the same.

  1. Power Automate: Remove/Restore Permissions Inheritance

Today, we will try to change the permission type of a user on list item. Let's start-

  1. The list has already been created in my last post (mentioned above).
  2. The Power Automate has already been created in our last post. We will use the same. 
  3. Add an action "Initialize variable" and name it "varUserID" of type Integer and value as 0.

  4. Now add another action "Send an HTTP request to SharePoint".
    1. Select the Site
    2. Choose Method as GET
    3. Uri as "_api/web/SiteUsers/getByEmail('<EmailID>')"
    4. Leave Headers as blank
    5. Leave Body as blank
  5. This will get the user information from site. We will fetch UserID from it's output.

  6. Now add another action named "Set variable". Select varUserID as Name and in Value put below formula "body('Send_an_HTTP_request_to_SharePoint_-_Get_UserID_By_EMail')?['d']['id']"

  7. This will set the user id to the variable.
  8. Now we have to set its permission on the item. For that, first we should know, the respective IDs of role definitions.
  9. Below is the list of few role definitions-
  10. Role Definition Name Role Definition ID
    Full Control 1073741829
    Design 1073741828
    Edit 1073741830
    Contribute 1073741827
    Read 1073741826
    View Only (System.LimitedView) 1073741924
    Limited Access 1073741825
  11. Now add another HTTP action and
    1. Select the site
    2. Select POST as Method
    3. in Uri box, paste the below formula-
      1. _api/lists/getByTitle('TestList')/items(@{triggerOutputs()?['body/ID']})/roleassignments/addroleassignment(principalid=@{variables('varUserID')},roledefid=1073741826)
        1. The orange highlighted part is the list name.
        2. The lime highlighted part is ID of list item.
        3.  The yellow highlighted is ID of user (varUserID).
        4. The aqua highlighted is the ID of role definition that you are providing the user upon that list item. 
    4. Leave Headers and Body as blank
  12. That's all.
  13. Note:- Remember, if you want to give unique permissions, then you need to first stop the inheritance. As in this post, we are using the same flow, we had created in last post, so you have to either remove the step "Send an HTTP request to SharePoint - Restore Inheriting Permissions" or set it's "Static Result (Preview)" as OK.
  14. Now save the flow and add an item in list.
  15. I had added an item with title "Reset Permission of User".


  16. Before execution of flow, it was inheriting the access from parent.
  17. After execution of flow, we can see that inherited permissions have been removed and only two users have unique permissions.
    1. Myself as Full Control
    2. Other User with Read Access (we have provided using MS Flow)
  18. Now, if you wish to remove permission of any user, you need to add another HTTP action-
    1. Site Address- Address of your site
    2. Method - POST
    3. Uri - _api/lists/getByTitle('TestList')/items(@{triggerOutputs()?['body/ID']})/roleassignments/removeroleassignment(principalid=@{variables('varUserID')},roledefid=1073741826)
      1. The orange highlighted part is the list name.
      2. The lime highlighted part is ID of list item.
      3. The yellow highlighted is ID of user (varUserID).
      4. The aqua highlighted is the ID of role definition that you want to remove for that user from that list item. 
    4. Leave Headers & Body as blank.
  19. Now execute the workflow.
  20. You will find that the access, we had provided in previous step has been removed.
  21.  
  22. If the user has multiple roles/permissions assigned, then only particular permission will be removed. But In case, if the user has only single permission and the same has been removed, then the user access will be completely removed from that list item.
  23. This way you can set/reset permissions of users on list item.
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.