Wednesday, July 28, 2021

Power Automate: CRUD Operations Using REST API - Part 1

Hello Friends,

Welcome back with another post on Power Automate. Have you ever thought to use REST API in Power Automate. You can get data in Power Automate using "Get Items" action. But if someone asks you to use REST API in Power Automate to Insert/Update/Delete/Read. Is it possible?

Yes, you can use REST API in Power Automate to get the data as well as for Insert / Update / Delete. Today, we will talk about "Send an HTTP request to SharePoint" action in Power Automate. This action is specific to apply all REST API actions in SharePoint and it's a free action unlike other HTTP action which are PREMIUM.

So, here, we will learn about CRUD operations using REST API, Power Automate and PowerApps. It will be a series of posts. Let's start with Create operation.

  1. Let's create a simple list "ListForRESTAPI" in SharePoint Online with only 2 columns-
    1. Title (Single line of text)
    2. PinCode (Number) (Decimal Places 0)


  2. Now click on Integrate >> Power Apps >> Create an app. It will ask you to provide a name for App. Let's give the name as "RESTAPICRUPApp". Click on "Create" button.




  3. The App will be created with Browse Screen, Detail Screen and Edit Screen (For New & Edit).


  4. In this post, we will work on "Add New Item". Therefore, we will choose EditScreen1 to perform our activities. But before that, we need a Power Automate that will be used to save the data in SharePoint list. This Power Automate will use REST API to perform the activity. 
  5. So, open Power Automate portal, click on "+ Create", then choose "Instant cloud flow".


  6. It will open a popup to choose the type of flow you want to create and also to provide a name to it. Choose "PowerApps" and give flow name as "HTTPCRUD-Create" and click on Create button.


  7. With this, we will get the Power Automate created as below-


  8. Now we will add steps and actions.
  9. First of all, we need the input values for "Title" and "PinCode" from PowerApps that will be saved into SharePoint List. For this, we will create 2 input parameters using Compose action.
  10. For this, click on "+ New step", a popup will open, type Compose in the input box provided to filter the Compose step. Click on the Compose step displaying in the filtered content. It will add Compose step in our Power Automate. Rename it to "ComposeTitle".
  11. Now we have to set it as a parameter that will take value from PowerApps. For this click on the Inputs text box of this Compose step and choose "Ask in PowerApps" from the Dynamic content list.


  12. It will create a input parameter named "ComposeTitle_Inputs" that will be reflected in PowerApps while using this Power Automate.
  13. Similar way, we will create another input parameter "ComposePinCode_Inputs"


  14. Now the major part arrives. We will now apply REST API step to insert the data in SharePoint list. For this, click on "+ New step" and type HTTP in search box. The search result will show the first option as "Send an HTTP request to SharePoint".


  15. Select this option. We will below screen-


  16. Choose the address of SharePoint site in "Site Address",
  17. Choose "POST" as the "Method".
  18. Fill in the "Uri" as "_api/web/lists/getbytitle('ListForRESTAPI')/items". Change the list name if you have chosen something else. 😉
  19. In Headers-
    1. Enter key as "content-type"
    2. Enter value as "application/json;odata=nometadata"
  20. Now comes to "Body" section. Provide a json format of input values with column names.
    1. For Title, select the Outputs of ComposeTitle
    2. For PinCode, select the Outputs of ComposePinCode


  21. Click on Save to save the Power Automate. This way Power Automate has been created. Now get back to PowerApps. So, go back to step no 7.
  22. Click on EditScreen1 and add a button named "btnAdd" and write its Text as "Add".


  23. Now update the formula for "OnSelect" action of this button. For this click on the formula box of OnSelect of btnAdd. Now click on "Action" >> "Power Automate". It will open a popup where all the PowerApps flows will appear. You will find the recently created Power Automate "HTTPCRUD-Create" here. Click on this flow. Once clicked, it will show a yellow bar with text as "Adding".


  24. Once added, the message will change and the Power Automate will be added in formula bar.


  25. As you see, the tooltip of the formula bar is showing the complete Power Automate (with it's input parameters). Now you need to just provide the value to it as per the sequence.


  26. It's done. Now you can add navigation back to list screen after this formula. With this the complete formula will be-


  27. 'HTTPCRUD-Create'.Run(
        DataCardValue3.Text,
        DataCardValue4.Text
    );
    Navigate(
        BrowseScreen1,
        None
    );
    
  28. Save the PowerApps and Let's try now to insert a data.
  29. When you execute the PowerApps, by default the BrowseScreen (list of records) will appear. As there is no record in list as of know, it will show blank screen.
  30. Click on + icon at top right corner. It will open the EditScreen in New Form mode.
  31. Input the Title and PinCode and click on Add button placed at bottom.
  32. PowerApps will call the Power Automate by passing the data we have input. Power Automate will save the data in SharePoint list. Once the process will complete, the Navigate action will executed and the app will redirect us back to Browse Screen.
  33. Here it may be possible that you still get the blank screen because the list collection is not refreshed. Therefore, click on Refresh icon placed at top. You will find that the screen is now showing the record, we had inserted just now.


  34. The record inserted is also reflecting in SharePoint list.


  35. With this, Part 1 of the series is over. We have learned the insertion of record using PowerApps, Power Automate and REST API.
  36. If you want to check the status of HTTP request in Power Automate and then also want to return the ID of record created along with Response message, please visit my Add-Ons Post-
    1. Power Automate: CRUD Operations Using REST API - Part 6 (Add-Ons) 
  37. In next Part, we will learn the Update feature.

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.