Hello Friends,
Welcome back with another post on Power Automate. In last 5 posts, we were discussing about CRUD operations using REST API in Power Automate with PowerApps. There, we had learnt about Create, Read, Update and Delete the data in SharePoint. In this post we will learn about some Add-Ons that can be used in filtering / sorting / pagination the data during READ execution. Before start, I would request you to go through my all last 5 posts whose links are given below-
- Power Automate: CRUD Operations Using REST API - Part 1
- Power Automate: CRUD Operations Using REST API - Part 2
- Power Automate: CRUD Operations Using REST API - Part 3
- Power Automate: CRUD Operations Using REST API - Part 4
- Power Automate: CRUD Operations Using REST API - Part 5
Let's start-
- CREATE ADD-ON-
- In my post (Part 1), we were talking about the item creation using Power Automate with REST API. If you wish to return the ItemID, Custom message back to PowerApps, please follow below steps-
- After the Action "Send an HTTP request to SharePoint", we need to capture the Status Code and Item ID. For this, add new action and choose Compose. Rename it "Compose (Status Code)". (Renaming is optional just to improve reading of code).
- Click on Inputs box and write the formula "outputs('Send_an_HTTP_request_to_SharePoint')?['body']['statusCode']" in expression box.
- This way, the Compose Output will hold the status code of HTTP request.
- Now, initialize a new variable (intNewItemID) of Integer type to hold the Item ID of newly inserted record. We will send this ID back to PowerApps.
- Next step is to add a string variable (strResponseMessage) to hold the message that we will return back to PowerApps.
- Now we will check the value of status code. For this, we will add a Condition and check if the value is 200 or 201. Details of HTTP REST API codes can be checked here HTTP Status Codes. If the condition matches, we will read the ID parameter from REST API response and set it to intNewItemID. Also, we will set our custome message to strResponseMessage.
- To set the value of intNewItemID, add an action "Set Variable" and in the Name box, choose the intNewItemID variable. In Value box, write the code "outputs('Send_an_HTTP_request_to_SharePoint')?['body']?['d']?['ID']".
- Once done, please save the flow and close it. After closing, reopen the flow. By doing so, the Value part written above get managed according the the appropriate syntax for flow. You may see the change in below screen shot (given for next variable).
- Now we will set the Response text. For this, add a new action "Set Variable". Choose the variable "strResponseText" in the name box and in value field, write your custom message. I have take the message as "Record created successfully.".
- For "No" condition, update the message.
- Now, at last, add the action "Respond to a PowerApp or flow" to send these values back to PowerApps.
- Here click on "+ Add an output" and then click on Text type. Write the title as "ID" and code "string(variables('intNewItemID'))" in expression box as response value and click on OK. We are sending the ItemId as string value back to PowerApps. You may send in numeric format also.
- Again click on "+ Add an output" and then click on Text type. Write the title as "Message" and select "strResponseMessage" variable as response value.
- Save the flow and use the same in PowerApps.
- In PowerApps, store the output of Power Automate in a variable. Then extract each value separately from this variable. For this the code is-
Set( PowerAutomateResponse, 'HTTPCRUD-CreateStatusCode'.Run( "Sachin Jain", "234789" ) ); UpdateContext({ItemID: PowerAutomateResponse.id}); UpdateContext({ResponseMessage: PowerAutomateResponse.message});
- This way, you can get the content from response. I had shown the output using Notify as below-
- You can check the response using Monitor.
- You can open Monitor using App Checker-
- This way you can send response back to PowerApps. The same way you can modify the UPDATE / DELETE part of flow to response back to PowerApps.
- READ ADD-ON-
- In my last post (Part 5), we were talking about getting the data from Power Automate using REST API. Till now, we haven't provided any filter. In case, if we have to get the filtered data then we can modify the Uri in "HTTPCRUD-Read" as below-
- Current- _api/web/lists/getbytitle('ListForRESTAPI')/items
- After applying Filter- _api/web/lists/getbytitle('ListForRESTAPI')/items?$filter=Title eq 'Sachin Jain'
- After applying this filter, we get the filtered data in PowerApps as below-
- You can pass this filter query from PowerApps itself. For this, you need to add an input parameter in Power Automate (say - ComposeFilterQuery_Inputs) and when clicking on get data, you have to pass your filter query from PowerApps to Power Automate. It's your wish that you send the complete filter query (?$filter=Title eq 'Sachin Jain') or just the query part (Title eq 'Sachin Jain').
- Remember, any change in Power Automate (adding / updating / deleting any input / output parameter), you need to add that Power Automate again in PowerApps code by clicking Action >> Power Automate >> PowerAutomateName. But this action has a drawback. The moment you add any Power Automate in code, it remove all the existing code from formula box. So make sure to copy the existing code in notepad and then add the updated Power Automate. Once added, replace the added Power Automate code line with the copied code. Now make changes in your code. Actually, when you click on Action >> Power Automate >> PowerAutomateName, the system adds the latest schema of that flow in system. Once the schema gets loaded, then you can just update the code by writing it manually.
- In both cases, the Power Automate will look like-
- Same way, we can apply sorting also. For this, add "?$orderby=Title" code at the end of Uri.
- The output is:
- This way, you can add more functionalities to CRUD operations using Power Automate and REST API.