Wednesday, July 28, 2021

Power Automate: CRUD Operations Using REST API - Part 4

Hello Friends,

Welcome back with another post on Power Automate. In last 3 posts, we were discussing about CRUD operations using REST API in Power Automate with PowerApps. There, we had learnt about Insert, Update and Delete the data in SharePoint. You may visit my these posts by clicking on below link-

  1. Power Automate: CRUD Operations Using REST API - Part 1
  2. Power Automate: CRUD Operations Using REST API - Part 2
  3. Power Automate: CRUD Operations Using REST API - Part 3
This is the next post of this series and the most crucial one because, in this post we will learn about on How to retrieve data from Power Automate using REST API in PowerApps. Power Automate does not provide any direct feature to send dataset as Response rather it can send only string data. Therefore, we have to do some workaround to send data to PowerApps. Then, in PowerApps, we have to apply some logic to capture this response and convert into Collection. In this post, we will work on Power Automate part and in next and the last post, we will work on PowerApps part. Let's start-

  1. Open the Power Automate portal and make a copy of the Power Automate flow, we had created in last post by clicking on "My flows". Then click on 3 dots showing ahead of flow "HTTPCRUD-Delete" and choose "Save As". It will ask you to provide the name of flow you want to make as a copy. give the name "HTTPCRUD-Read" and click on Save.


  2. It will create a copy of the flow and by default retain it in Turn off mode. Click on 3 dots ahead of this flow and choose Turn on.


  3. Again click on 3 dots and click on Edit.
  4. As we have to read the records from SharePoint list and send them to PowerApps, hence, we need no input parameter for Power Automate. We will directly choose "Send an HTTP request to SharePoint" step.
  5. Therefore, remove all the input parameters appearing in Power Automate.


  6. Now expand the step "Send an HTTP request to SharePoint".
  7. Change the Method from DELETE to GET.
  8. Remove all key-value pairs from Headers.
  9. Replace the Uri with "_api/web/lists/getbytitle('ListForRESTAPI')/items". As we have to get the data hence no need to pass any item id.
  10. Now, the Power Automate will look like-

  11. Now add "Initialize variable" step and give the Name as "strIntermediateOutputString". This variable will be used to hold the delimiter based values that we have to send back to PowerApps.

  12. Now the process is that we have to -
    1. First combine the values of each column of an item using a delimiter (call it as Value Delimiter). Let's take "~" (tild) sign as value delimiter.
    2. Then combine each string of above step with another delimiter (Item Delimiter). Let's take "|" (pipe) sign as item delimiter.
    3. For example Sachin Jain~110045|Ramesh Gupta~201001. Here Sachin Jain~110045 and Ramesh Gupta~201001 are two separate items in list and there are separated with each other by pipe (|) sign. While in each item the values of Title column and PinCode column are separated by tild (~) sign. So, we have to create this type of string from the output of "Send an HTTP request to SharePoint" step. For this, we will use "Apply to each" step.
  13. Click on + New step and choose "Apply to each" action.
  14. Click in the box provided to "Select an output from previous step" and in the popup just opened, paste below expression in Expression box "outputs('Send_an_HTTP_request_to_SharePoint')?['body']?['d']?['results']".
  15. Here we are asking to Power Automate to get the results available in body.d.results as input. The "Send_an_HTTP_request_to_SharePoint" is the name of HTTP action you had used above. In case of any name change, change here also (also replace blank spaces by underscore)


  16. As we have to recursively append the string hence now we will add "Append to string variable" action. It will append the data as per our desire (we have shown in sample above). Now select the string variable (we have declared above) in Name dropdown.


  17. The value section will contain a formula which will concatenate the Title and PinCode values delimited by "~" sign and then concatenate the "|" sign at the end of each loop.
  18. But before that just type any static value in "Value" box and save the flow. Then close the flow and reopen it.
  19. The reason behind doing this activity is that without saving and closing the "Apply to each" action didn't reflected in Dynamic content section. Once you reopen it, you will find that the display of "previous output" box appearing now in different mode.


  20.  And when you click on "Value" box of "Append to string variable" action, you will find that in Dynamic content window, "Apply to each" starts appearing.


  21. The formula will be-
    1. concat(items('Apply_to_each')['Title'],'~',items('Apply_to_each')['PinCode'],'|')


  22. This way, we had concatenated all the information required to send back to PowerApps. Now we will remove the last pipe sign as it is unwanted (otherwise it will create a problem while converting the data to collection in PowerApps).
  23. Use the Compose action with substring and sub (subtract) expression to remove the last character. 
  24. The formula is-
    1. substring(0,sub(length(variables('strIntermediateOutputString')),1))


  25. Now, we have to send this data back to PowerApps. For this, click on "+ New step" and add action "Respond to a PowerApp or flow".


  26. It will ask you to add an output. Click on that link "+ Add an output". It gives 6 types of output-
    1. Text
    2. Yes/No
    3. File
    4. Email
    5. Number
    6. Date
  27. As you can see, there is no option to send the dataset/object, we have created the string of data.
  28. Now, choose Text option. It will ask you to provide the title and the value you need to send back.
  29. Let's give the Title as "ResultSet" and in value field, choose the Outputs of Compose action just defined above.


  30. This way, we have completed the design part of Power Automate. now click on Save to save the changes.
  31. You may take a test of this Power Automate by clicking on Test >> Manually >> Test >> Run Flow >> Done.
  32. The output of Compose action which is sent to PowerApps comes out to be as-

  33. This is the output we need to send back to PowerApps. Here we know that the first part of item is Title and second part is PinCode.
  34. With this, Part 4 of the series is over. We have learned to get the data from SharePoint using REST API in Power Automate and then did some work around to convert it into string and sent back to PowerApps.
  35. The next (last) part of this series is the final post of this series where we will implement our logic to convert the string received from Power Automate into collection.
  36. Stay tuned...
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.