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 !

Sunday, August 8, 2021

Power Automate: Remove/Restore Permissions Inheritance

 Hello Friends,

Welcome back with another post on Power Automate. We are going to start another series of Power Automate upon Permissions at item level. In this post, we will discuss about -

  1. Stop Inheriting Permissions
  2. Delete Unique Permissions (Restore Inherited Permissions)
Let's take one by one.
  1. Stop Inheriting Permissions-
    1. First of all create a list named "TestList". There would a default column supplied "Title".
    2. Click on App Launcher (9 dots) at top left corner and then click on Power Automate.
    3. Click on "+ Create" and then choose "Automated cloud flow".
    4. A popup will appear. Give you flow a suitable name and then select the trigger as "When an item is created (SharePoint)".
    5. Provide the Site Address and the List Name upon which you need to perform action.
    6. Then add an action Compose to save the list name as we have to use the list name repeatedly therefore, we will use the output of this compose action. Rename this action as "Compose - ListName".


    7. Now add one more action "Send an HTTP request to SharePoint". Rename it as "Send an HTTP request to SharePoint - Stop Inheriting Permissions". This action has 5 parts-
      1. Site Address - Provide Site Address
      2. Method - Choose POST
      3. Uri - _api/lists/getByTitle('@{outputs('Compose_-_ListName')}')/items(@{triggerOutputs()?['body/ID']})/breakroleinheritance(copyRoleAssignments=false,clearSubscopes=true)
        1. The lime highlighted part is ListName. We will use the Outputs of Compose action.
        2.  The aqua highlighted is Item ID. We will use the ID from When an item is created action.
      4. Headers - Leave Blank
      5. Body - Leave Blank


    8. Save the flow and come back to SharePoint list. Add an item.
    9. Now quickly check the permissions of this item. You will find the item is inheriting parent list permissions.
    10. Now check the execution status of flow. Once it shows success, then check the permissions again.
    11. You will find that inheritance of permissions has been stopped. Only you will be having the access on this list item.
    12. Apart from you, Site Collection Owners also have permissions on this list item. Rest all permissions have been removed.
    13.   
    14. This way you can stop inheritance of permissions.
    15. Now, what if, we have to restore the permissions. Here is the solution.
  2. Delete Unique Permissions (Restore Inherited Permissions)-
    1. In section #1, we have seen how to stop inheritance of permissions. Now some times, we need to restore the inheritance. So, here we will see the implementation of the same.
    2. In above flow, add one more action "Send an HTTP request to SharePoint". Rename it as "Send an HTTP request to SharePoint - Restore Inheriting Permissions". This action has 5 parts-
      1. Site Address - Provide Site Address
      2. Method - Choose POST
      3. Uri - _api/lists/getByTitle('@{outputs('Compose_-_ListName')}')/items(@{triggerOutputs()?['body/ID']})/ResetRoleInheritance
        1. The lime highlighted part is ListName. We will use the Outputs of Compose action.
        2.  The aqua highlighted is Item ID. We will use the ID from When an item is created action.
      4. Headers - Leave Blank
      5. Body - Leave Blank
    3. Save the flow and click on Test >> Automatically >> With a recently used trigger >> Succeeded.

    4. The Test feature allows us to use the same list item. As we already stopped the inheritance, therefore, using the same list item will give us the visible result.
    5. Now, click on the Test button provided at the bottom. It will execute the flow. Wait for a couple of moments to perform the activity by flow. A few moments later you will get the screen like below 😉


    6. Now go back to the permissions page and refresh the same. You will be surprised to see that unique permissions have been vanished. The item again started inheriting permissions. Wow!

    7. This way, you can Set/Reset the Permissions Inheritance.
    8. In case, if you wish to remove only particular group then you need to use below Uri-
      1. _api/web/lists/getByTitle('[LIST_TITLE]')/items([ItemID])/roleAssignments/groups/removebyid([Group_PRINCIPLE_ID])]
  3. Key Points-
    1. Stopping Permissions Inheritance removes all the permissions except-
      1. Creator of that item
      2. Site Collection Admins
With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.

Stay Safe !
Stay Healthy !

Power Automate: Send Multiple Attachments In Approval EMail

Hello Friends,

Welcome back with another post on Power Automate. In my last post, we had tried to send multiple attachments (uploaded in a list item) in email through Power Automate. In last, I asked you, what if you have to send all these attachments in "Start and wait for approval" email? Hope you have already implement. Anyways, let's try-

  1. First of all, please visit my last post to create the list and the power automate.
    1. Power Automate: Send Multiple Attachments In EMail
  2. Now, the way, we sent the attachments in an email is little bit different to what we have to do for approval mail action.
  3. For simple Email, the array format was:-
    1. {
      "Name":
      "ContentBytes":
      }
      
  4. For approval email, if you use the same format, it will through error of ContentBytes. Therefore, we will change this format. For approval email, we will use below format.
    1. {
      "Name":
      "content":
      }
      
  5. Rest things will be same. Means-
    1. Declare a variable of Array type using "Initialize variable" action with name as "MultipleAttachmentsArrayForApproval".


    2. Use "Apply to each", "Get attachment content" and "Append to array variable" actions to create the array of attachment contents.


    3. Now add action "Start and wait for approval" and input all the required information. Add this variable in attachments section. For this, please read my last post whose link is given above. We will apply same process here (see point no 27 of last post).

  6. All done. Save the workflow and test it by adding a new item in SharePoint list. Well, I have already one, so I will test in it.
  7. I had created a list item with 4 attachments-

  8. Just clicked on Save button. After waiting a couple of minutes, I got a mail in my mailbox.

  9. Wow, I got a mail to approve or reject the item request. The interesting part is that, all 4 attachments are also got delivered.
  10. This way, you can send attachments in approval email also.
  11. Please note that these attachments will show only in mail box. If you are using MS Teams, then you will get approval request there also but without attachment. Even, if you are using your mobile to approve/reject the request then their also you will not find the attachments.

  12. This way, you can send multiple attachments with Email as well as Approval Flow EMail.
With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.

Stay Safe !
Stay Healthy !

Power Automate: Send Multiple Attachments In EMail

 Hello Friends,

Welcome back with another post on Power Automate. Have you ever face any requirement where all the attachments of any list item needs to send through Email? Let's suppose a user submits a request in system along with multiple attachments. Now the details of this request needs to by sent to his/her manager along with the attachments, he/she has uploaded. How will you do? No Idea, let's try:-

  1. I have created a list "TestList" with only one column i.e. "Title".

  2. Let's create a Power Automate. For this click on App Launcher (9 dots) at top left corner and choose "Power Automate".
  3. Click on "+ Create" >> "Automated cloud flow". It will show a popup window where you need to input the name of the workflow. Then choose the type of trigger upon which this workflow will execute.
  4. Choose "When an item is created (SharePoint)".

  5. Click on "Create".
  6. It will create a blank workflow with a default action "When an item is created". Choose the SiteName and the list name upon which you need to execute this workflow.


  7. Now take a pause. First discuss how we will perform next. First we need to 
    1. Get all attachments list
    2. Get content of each attachment and make an array of the same with File Name and ContentBytes.
    3. Send this array as attachment in Email.
  8. Let's start-
  9. Choose an action Get attachments


  10. Now define a variable of Array type.


  11. Now we will use Apply to each action to get attachment contents and append in array.
  12. For this, first we will add action "Get attachment content" and fill properties as below. The Id field will be mapped with the Item ID (when an item is created) while File Identifier will be mapped with the Id (Get attachments).


  13. The moment you bind File Identifier field, you will see that "Apply to each" action automatically binds with the Get attachment content action.


  14. Now, add one more action "Append to array variable". We will append all attachments in the variable defined earlier.


  15. The format of array is-
    1. {
      "Name":
      "ContentBytes":
      }
      
  16. Now the last part. Add an action "Send an email (V2)". Specify the To, Subject, Body as per the requirement. Once done, click on "Show advanced options".


  17. It will expand the window and will show Cc, Bcc, Attachments options. By default the system provides to give display name and content separately. But as we have the array here, so we will click the toggle icon showing ahead to Attachments section. This toggle icon toggle the attachments linking process between single to array and vice versa.

  18. After toggle, click on box provided for attachments and select the array variable.
  19. That's all. Save the flow and test the same.
  20. To get the same tested, I came back to the SharePoint list and added one list with 4 attachments
    1. .txt file
    2. .docx file
    3. .pdf file
    4. .png file


  21. First, I check the Run history of workflow. I found it ran successfully.

  22. Now, I will check my mail box.
  23. Wow, I had an email in my inbox with the same attachments I had uploaded.


  24. This way, you can, send all the attachments, user has uploaded in system.
  25. With this, we have achieved our goal.
  26. Now, I am giving you a task. Let us suppose, I have to send all these attachments in approval mail "Start and wait for approval", then how would you do that? Try it.
  27. I will, show the same in my next post.
    1. Power Automate: Send Multiple Attachments In Approval EMail
With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.

Stay Safe !
Stay Healthy !