Please let me know what is the problem in the structure of the code given below:
//Deleting existing data items to SharePoint list using $batch
Feb 10 2021 02:32 AM
Please let me know what is the problem in the structure of the code given below:
//Deleting existing data items to SharePoint list using $batch
function deleteListItems (spListName, data, goBackToParent) {
var queueNo = 1;
var totalQueue = Math.ceil(data.length / 150);
gettingRecords_ToDelete(queueNo);
function gettingRecords_ToDelete(page) {
var getData = getQueue(data, page, 150);
deletingRecords(getData.data, page);
}
function deletingRecords(data,page){
console.log("deletingRecords ==>");
console.log("queueNo : " + page);
console.log("totalQueue : "+totalQueue);
console.log(data);
var batchGuid = generateGUID();
var changeSetGUID = generateGUID();
var batchContents = new Array();
for (var i = 0; i < data.length; i++) {
if (data[i] == undefined) {
return;
}
// create the request endpoint
var endpoint1 = _spPageContextInfo.webAbsoluteUrl
+ '/_api/web/lists/getbytitle(\'' + spListName + '\')'
+ '/items(' + data[i].ID + ')';
// create the changeset
batchContents.push('--changeset_' + changeSetGUID);
batchContents.push('Content-Type: application/http');
batchContents.push('Content-Transfer-Encoding: binary');
batchContents.push('');
batchContents.push('DELETE ' + endpoint1 + ' HTTP/1.1');
batchContents.push('Content-Type: application/json;odata=verbose');
batchContents.push('Accept: application/json;odata=verbose');
batchContents.push('IF-MATCH: *');
batchContents.push('');
}
batchContents.push('--changeset_' + changeSetGUID + '--');
var batchBody = batchContents.join('\r\n');
batchContents = new Array();
batchContents.push('--batch_' + batchGuid);
batchContents.push('Content-Type: multipart/mixed; boundary="changeset_' + changeSetGUID + '"');
batchContents.push('Content-Length: ' + batchBody.length);
batchContents.push('Content-Transfer-Encoding: binary');
batchContents.push('');
batchContents.push(batchBody);
batchContents.push('');
batchContents.push('--batch_' + batchGuid + '--');
batchBody = batchContents.join('\r\n');
var endpoint = _spPageContextInfo.webAbsoluteUrl + '/_api/$batch';
var batchRequestHeader = {
'X-RequestDigest': $("#__REQUESTDIGEST").val(),
'Content-Type': 'multipart/mixed; boundary="batch_' + batchGuid + '"'
};
$.ajax({
url: endpoint,
type: 'POST',
async: false,
headers: batchRequestHeader,
data: batchBody,
success: function (response) {
// goBackToParent(true, response);
queueNo++;
if (queueNo <= totalQueue) {
gettingRecords_ToDelete(queueNo);
} else {
console.log("Records Deleted successfully !");
console.log("queueNo : "+queueNo);
console.log("totalQueue : "+totalQueue);
goBackToParent(true, response);
}
},
error: function (error) {
goBackToParent(true, error);
}
});
}