June 10, 2021

How to implement SharePoint CRUD Operation using Power Automate?

Introduction

In this article, we will talk about how we can Create, Update and Delete data using “Send an HTTP request to SharePoint” Action in Power Automate. 

Real-Life Business Use Case:

We all know with OOTB Power Automate action - “Send an HTTP request to SharePoint” we can perform SharePoint CRUD operations easily. However, sometimes, there are situations where we need to set up one flow for a different site. For this scenario, we need to pass Site URL and List Name Dynamically. At that time, we can use OOTB Power Automate action - “Send an HTTP request to SharePoint” and pass Site Name and List Name dynamically. In this article, we will see the most used methods, such as GET, POST, PATCH, and DELETE with examples.

Prerequisites:

Log in to the flow portal with your Office 365 credentials.
For this article, we have created two SharePoint List. One is for Training and the second List for PostData

Overall Architecture/Schema of Lists:

Below is the column structure of the Training List.
 
Below is the column structure of the PostData List.
 
Now, let’s get started with development procedure!

Step 1:

Add Manual Trigger. We can choose trigger based on business need.

Step 2: 

Initialize variables for site URL and list name.

Step 3: Get Items

Click on New Step and add an action “Send an HTTP request to SharePoint”. 
    • Site Address: Choose your SharePoint Site from the dropdown.
    • Method: Select “Get”
    • Uri: _api/web/lists/getbytitle(“List Name”), other attributes like a top, filter, etc can be added here.

Step 4: Create Item 

Add “Send an HTTP request to SharePoint” action.
  • Site Address: Choose your SharePoint Site from the dropdown.
  • Method: POST
  • Uri: _api/web/lists/getbytitle(“List Name”)
  • Headers: In the case of a post we need to add a header. In the Headers section, there is a key-value pair combination.
    • content-type :application/json;odata=verbose
    • If-Match :  *
  • Body: JSON body as per business requirements.
     {  
     "__metadata": { "type": "SP.Data.PostDataListItem" },  
     "Title": "New Demo User Post",  
     "Score": 2 ,  
     "TrainingId": 4    
     }  
    
Note: The body part will have a schema of the list item you want to create. Make sure you are including all mandatory columns in the schema. Now, run the flow and check the result.
Success code: 201 means our post method completed successfully. Now we will verify the SharePoint list to check.

Step 5: Update Item

To Update the existing item, we need to use Patch method. Add an Action “Send an HTTP request to SharePoint”.
  • Site Address: Choose your SharePoint Site from the dropdown.
  • Method: PATCH
  • Uri: _api/web/lists/getbytitle(“List Name”)(ItemID) , id of item which will be updated
  • Headers: In the case of a post we need to add a header. In the Headers section, there is a key-value pair combination.
    • content-type :application/json;odata=verbose
    • If-Match : *
  • Body: JSON Body as per the business requirements.
     {   
     "__metadata": { "type": "SP.Data.PostDataListItem" },   
     "Title": "New Demo",   
     "Score": 2 ,   
     "TrainingId": 4   
     }  
    
Success code: 204 means we have successfully updated the specific list item. Now let’s verify the list item.
We can see that the User Department is updated to HR from IT for Demo User 1.

Step 6: Delete Item

Add an Action “Send an HTTP request to SharePoint” in our existing flow. 
  • Site Address: Choose your SharePoint Site from the dropdown.
  • Method: DELETE
  • Uri: _api/web/lists/getbytitle(“List Name”)(ItemID) , id of item which will be updated
  • Headers: In the case of a post we need to add the header. In the Headers section, there is a key-value pair combination.
    • content-type :application/json;odata=verbose
    • If-Match : *
    • X-Http-Method: DELETE
We are going to target the Item id 4 to delete using the action “Send an HTTP to SharePoint”. Now, run the flow and check the result.

Conclusion:

This is how we can easily implement CRUD operations for SharePoint List item using Power Automate. Isn’t that amazing?
If you have any questions you can reach out our SharePoint Consulting team here.

No comments:

Post a Comment