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.
-
- var arr=[
- {
- reqUrl:"https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items"
- ,action:"ADD",
- data:{__metadata:{type:"SP.Data.PlaceHolderListListItem"},Title:"Add Article_Using_Batch_1"}},
- {
- reqUrl:"https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items"
- ,action:"ADD",
- data:{__metadata:{type:"SP.Data.PlaceHolderListListItem"},Title:"Add Article_Using_Batch_2"}}
- ];
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.
- BatchUtils.PostBatchAll({rootUrl:"https://brgrp.sharepoint.com",
- 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.
See the created items in SharePoint List.
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.
- var arr=[
- {
- reqUrl:"https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items(649)"
- ,action:"UPDATE",
- data:{__metadata:{type:"SP.Data.PlaceHolderListListItem"},Title:"Update Article_Using_Batch_2"}},
- {
- reqUrl:"https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items(648)"
- ,action:"UPDATE",
- data:{__metadata:{type:"SP.Data.PlaceHolderListListItem"},Title:"Update Article_Using_Batch_1"}}
- ];
-
- BatchUtils.PostBatchAll({rootUrl:"https://brgrp.sharepoint.com",
- batchUrls:arr}).then(r=>console.log(r));
The output of the above batch request on SharePoint can be seen as below.
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.
-
- var arr=[{
- reqUrl:"https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items(212)"
- ,action:"UPDATE",
- data:{__metadata:{type:"SP.Data.PlaceHolderListListItem"},Title:"Update Article_1"}},
- {
- reqUrl:"https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items(213)"
- ,action:"UPDATE",
- data:{__metadata:{type:"SP.Data.PlaceHolderListListItem"},Title:"Update Article_2"}},
- {
- reqUrl:"https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items(214)"
- ,action:"UPDATE",
- data:{__metadata:{type:"SP.Data.PlaceHolderListListItem"},Title:"Update Article_3"}},
- {
- reqUrl:"https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items"
- ,action:"ADD",
- data:{__metadata:{type:"SP.Data.PlaceHolderListListItem"},Title:"Add Article_1"}}
- ,{
- reqUrl:"https://brgrp.sharepoint.com/_api/Lists/Getbytitle('PlaceHolderList')/items(215)"
- ,action:"DELETE"}];
-
-
- BatchUtils.PostBatchAll({rootUrl:"https://brgrp.sharepoint.com",
- 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.