We are trying to copy contents from one column to the other , However we get request digest error after 6K Items.
Below is the code
$(document).ready(function () {
$("#btnSubmit").click(function () {
GetItemId();
});
});
function UpdateListItemUsingItemId(Id, Ptype) {
//var deferred = $.Deferred();
//console.log(base64Img);
var _listItem = {
"__metadata": { 'type': 'SP.Data.PCFHeaderListItem' },
"PType": Ptype
};
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('DBSDistributorList')/Items(" + Id + ")",
method: 'POST',
contentType: "application/json;odata=verbose",
async: false,
data: JSON.stringify(_listItem),
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"content-Type": "application/json;odata=verbose",
"X-Http-Method": "MERGE",
"If-Match": "*"
},
success: onSuccess,
error: onError
/*success: function (data) {
deferred.resolve(data);
},
error: function (err) {
deferred.reject(err);
}*/
});
//return deferred.promise();
function onSuccess(data) {
console.log('List Item Updated');
}
function onError(error) {
console.log(JSON.stringify(error));
}
};
function GetAllItems(requestUrl) {
var deferred = $.Deferred();
$.ajax({
url: requestUrl,
method: "GET",
headers: {
"accept": "application/json;odata=verbose",
}
}).done(function (thisResult) {
if (thisResult.d.__next) {
// if the __next property is present,
// that measn that there's more to get,
// so don't start resolving things yet,
// just make the next request
var nextRequest = GetAllItems(thisResult.d.__next);
// since GetAllItems returns a promise,
// we have to wait for that promise to resolve
// before resolving the promise we made
// at the beginning of this function
nextRequest.done(function(nextResult) {
// nextResult should be a plain array of items,
// so we need to combine that with the array
// of items we got in this call in thisResult.d.results
var combinedResults = thisResult.d.results.concat(nextResult);
// then resolve the combined results back up to the next level
deferred.resolve(combinedResults);
}).fail(function(error) {
deferred.reject(error);
});
} else {
// __next doesn't exist, so
// there's nothing more to get, so
// start resolving the nested promises
deferred.resolve(thisResult.d.results);
}
}).fail(function (result) {
deferred.reject(result);
});
return deferred.promise();
};
function GetItemId()
{
var initialUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('PCFHeader')/Items?$select=ID,Project_x0020_Type";
// start the process
var getAllItemsRequest = GetAllItems(initialUrl);
// wait for it all to complete
getAllItemsRequest.done(function (allItems) {
// allItems should now be
// an array of all 700+ items
// make sure your request digest value is good
// using the built-in SP provided function for doing just that
UpdateFormDigest(_spPageContextInfo.webServerRelativeUrl, _spFormDigestRefreshInterval);
$.each(allItems, function (index, row) {
UpdateListItemUsingItemId(row["ID"], row["Project_x0020_Type"]);
});
}).fail(function (error) {
console.warn("Error getting all items");
console.warn(JSON.stringify(error));
});
}
Error
"The security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again."
Please advice
Thanks in advance.