SharePoint Online Batch API Request For Bulk CRUD Operation

Introduction

 
In this article, we will learn about creating SharePoint Online Batch API Request for INSERT, UPDATE, and DELETE operations using $batch REST API.
 

Description

 
In the previous article, we have learned how to use and make SharePoint $batch Rest API calls easily with a few lines of code using BatchUtils. You can reference Batchutils from previous article or you can directly reference from BatchUtils.
 
Once you have added BatchUtils reference, add code as below for Bulk Insert in SharePoint Online List.
 
Note
BatchUtils is in development feedback and suggestions are welcome 
 

BULK INSERT using SharePoint Online $batch REST API with BatchUtils 

 
Step 1
 
Prepare a Request URL with Payload and action as array for multiple requests.
  1. // Prepare collection of request with requestUrl and payload data.  
  2. var arr=[  
  3. {  
  4.    reqUrl:"https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items"  
  5.    ,action:"ADD",  
  6.    data:{__metadata:{type:"SP.Data.PlaceHolderListListItem"},Title:"Add Article_Using_Batch_1"}},  
  7.    {  
  8.       reqUrl:"https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items"  
  9.       ,action:"ADD",  
  10.       data:{__metadata:{type:"SP.Data.PlaceHolderListListItem"},Title:"Add Article_Using_Batch_2"}}  
  11. ];  
As per the above example reqUrl, the action and data are required field.
 
Supported action values are "ADD", "UPDATE",  and "DELETE".
 
Step 2
 
Pass the rootUrl and an array of requestUrl with payload.
  1. BatchUtils.PostBatchAll({rootUrl:"https://brgrp.sharepoint.com",  
  2. batchUrls:arr}).then(r=>console.log(r));  
As per the above code, the rootUrl and batchUrl fields are required in BatchUtils.PostBatchAll. rootUrl is used internally to create the Request Digest for $batch API REST Call.
 
You can see the response of batch request as per the below screenshot.
 
SharePoint Online Batch API Request For Bulk CRUD Operation
 
See the created items in SharePoint List.
 
SharePoint Online Batch API Request For Bulk CRUD Operation
 
As per the above request, we have added two items in a SharePoint list and the response is returned for both items.
 

BULK UPDATE or DELETE Using SharePoint Online $batch REST API pasting with BatchUtils

 
Prepare a collection of requests with requestUrl and payload data and set the action field as below.
  1. var arr=[  
  2. {  
  3.    reqUrl:"https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items(649)"  
  4.    ,action:"UPDATE",  
  5.    data:{__metadata:{type:"SP.Data.PlaceHolderListListItem"},Title:"Update Article_Using_Batch_2"}},  
  6.    {  
  7.       reqUrl:"https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items(648)"  
  8.       ,action:"UPDATE",  
  9.       data:{__metadata:{type:"SP.Data.PlaceHolderListListItem"},Title:"Update Article_Using_Batch_1"}}  
  10.    ];  
  11.      
  12. BatchUtils.PostBatchAll({rootUrl:"https://brgrp.sharepoint.com",  
  13. batchUrls:arr}).then(r=>console.log(r));  
The output of the above batch request on SharePoint can be seen as below.
 
 SharePoint Online Batch API Request For Bulk CRUD Operation
 
You can find similar code for Bulk Update operations with only  the"action" field changed. Yes, you are right. In BatchUtils, I have managed generation of Request Payload and Request Header based on "action" value.
 
The above same code can also work with the Bulk Delete operation by just changing the "action" to "DELETE".
 
SharePoint $batch REST API also supports multiple different operations with a single Batch Request.
 
See the sample request example which contains ADD/UPDATE/DELETE operations with single batch request.
  1. // Prepare collection of request with requestUrl and payload data.  
  2. var arr=[{  
  3. reqUrl:"https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items(212)"  
  4. ,action:"UPDATE",  
  5. data:{__metadata:{type:"SP.Data.PlaceHolderListListItem"},Title:"Update Article_1"}},  
  6. {  
  7. reqUrl:"https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items(213)"  
  8. ,action:"UPDATE",  
  9. data:{__metadata:{type:"SP.Data.PlaceHolderListListItem"},Title:"Update Article_2"}},  
  10. {  
  11. reqUrl:"https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items(214)"  
  12. ,action:"UPDATE",  
  13. data:{__metadata:{type:"SP.Data.PlaceHolderListListItem"},Title:"Update Article_3"}},  
  14. {  
  15. reqUrl:"https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items"  
  16. ,action:"ADD",  
  17. data:{__metadata:{type:"SP.Data.PlaceHolderListListItem"},Title:"Add Article_1"}}  
  18. ,{  
  19. reqUrl:"https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items(215)"  
  20. ,action:"DELETE"}];  
  21.   
  22.   
  23. BatchUtils.PostBatchAll({rootUrl:"https://brgrp.sharepoint.com",  
  24. batchUrls:arr}).then(r=>console.log(r))  
Reference Links,
  • https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/make-batch-requests-with-the-rest-apis
  • http://anomepani.github.io
  • https://social.technet.microsoft.com/wiki/contents/articles/30044.sharepoint-online-performing-batch-operations-using-rest-api.aspx
  • https://www.vrdmn.com/2016/08/batch-rest-requests-in-spfx-using.html 

Conclusion

 
In this article, we have learned about the usage of SharePoint $batch API with a minimal line of code using BatchUtils for INSERT, UPDATE, and DELETE Operations.