Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save anomepani/a5546fb2596bf387dfc31c88bf220879 to your computer and use it in GitHub Desktop.
Save anomepani/a5546fb2596bf387dfc31c88bf220879 to your computer and use it in GitHub Desktop.
/*
Below code depends on two utility SPOHelper and BatchUtils
For SPOHelper -> https://anomepani.github.io/posts/spohelper-sharepoint-online-rest-api-crud-operation-utility-with-example/
For BatchUtils -> https://anomepani.github.io/posts/make-batch-request-with-rest-api-in-sharepoint-online-sharepoint/
You can skip SPOHelper if you want and instead of that you can right your own logic to get Request digest pass to below methods
*/
//Step 1: Initial Setup
var rootUrl = "https://brsupport.sharepoint.com/sites/Training";
var reqUrl = rootUrl + "/_api/web/Lists/GetbyTitle('InsertList')/items";
//Step 2 : Prepare list of urls in array
var arr = [];
for (var index = 1; index <= 40000; index++) {
//PREPAR ARRAY OF 40000 ITEM FOR BULK INSERTIONS
arr.push({
reqUrl: reqUrl,
action: "ADD",
data: { Title: "Article_" + index },
});
}
//https://github.com/anomepani/sp-rest-util/blob/master/BatchUtils.ts
var processedCount=0;
CallBulkInsertOperations = (rootUrl, arr) => {
//GET REQUEST BEFORE MAKING BATCH REQUESTS
GetDigest(rootUrl+"/_api/contextinfo").then((r) => {
console.log(r);
var BatchOf100s = [];
for (var startIndex = 0; startIndex <arr.length; startIndex++) {
BatchOf100s.push(arr[startIndex]);
if (BatchOf100s.length == 100) {
CallBatchOperationUsingBatchUtils(rootUrl, BatchOf100s, r);
//After Batch Of 100s record processed, Set Empty array for next array execution
BatchOf100s = [];
}
}
});
};
function CallBatchOperationUsingBatchUtils(rootUrl, BatchOf100s, r) {
//T
setTimeout(() => {
BatchUtils.PostBatchAll({
rootUrl,
batchUrls: BatchOf100s,
FormDigestValue: r.FormDigestValue,
}).then((r) => {
console.log(r);
console.log("Processed..=>" + BatchOf100s.length);
}).catch(e=>{
console.log(e,"Retry is error occurs due to 429 - Too Many Requests");
setTimeout(()=>{
CallBatchOperationUsingBatchUtils(rootUrl, BatchOf100s, r);
},3000);
});
}, 1000);
}
//Uncomment below method to test
//CallBulkInsertOperations(rootUrl,arr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment