I am trying to add 5000 items in a SharePoint list using JSOM. But unfortunately didn't get any luck.
function createListItem() {
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('DummyList');
var itemCreateInfo = new SP.ListItemCreationInformation();
for (var i = 0; i < 5000; i++) {
this.oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('ItemNumber', i);
oListItem.update();
}
clientContext.load(oListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded() {
console.log('Item created: ' + oListItem.get_id());
}
function onQueryFailed(sender, args) {
console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
But after some time server stops responding. I know there is something called Threshold limit. But according to the requirement more than 5000 items should be stored in one take only. I have no idea where I am making mistake. Please help.
Somehow I found the solution for this. Instead of call back method I used REST API like this
function RestAdd()
{
for(var i = 0 ; i < 5000; i++)
{
$.ajax
({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('DummyListForMultiAdd')/items",
type: "POST",
async: false,
data: JSON.stringify
({
__metadata:
{
type: "SP.Data.DummyListForMultiAddListItem"
},
ItemNumber: i
}),
headers:
{
"Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "POST"
},
success: function(data, status, xhr)
{
console.log("success: "+i);
},
error: function(xhr, status, error)
{
console.log("failed: "+i);
}
});
}
}
What I did is, I just used REST API with async:false
. It adds your list items in a sync manner. In JSOM it works in async.