1

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.

3
  • You can try REST api with async:false so it will add your list items in a sync manner. In JSOM it works in async mode that's why you might be facing error. Commented Dec 8, 2017 at 4:45
  • Could you please give one small example @Dikesh Gandhi ?
    – Harsh
    Commented Dec 8, 2017 at 6:00
  • Please check my answer. Commented Dec 8, 2017 at 6:10

1 Answer 1

3

You can try with REST API with sync manner. Please see code as below:

for(var i=0;i<5000;i++)
{
    var item = {
    __metadata: { "type": "SP.Data.ABCListItem"},
    ItemNumber: i.toString()
    };

    $.ajax({
    url: fullurl +"/_api/web/lists/getbytitle('ABC')/items",
    type: "POST",
    async:false,
    contentType: "application/json;odata=verbose",
    data: JSON.stringify(item),
    headers: {
        "Accept": "application/json;odata=verbose",
        "X-RequestDigest": $("#__REQUESTDIGEST").val()
    },
    success: function(data) {
        alert('Success');
    },
    error: function(data) {
       console.log(data);
    }
    });
}

Reference: https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-lists-and-list-items-with-rest

1
  • 1
    Sorry for the late response. I tried it and its working. Thanks Dikesh Gandhi.
    – Harsh
    Commented Dec 11, 2017 at 4:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.