3
var itemArray = [];
for (var i = 0; i < data.length; i++) {
                var items = data[i].split(",");
                downloadID = items[0].trim();
                noOfDownloads = items[1];

                                        var itemCreateInfo = new SP.ListItemCreationInformation();
                        var oListItem = oList.addItem(itemCreateInfo);
                        oListItem.set_item('DownloadID', downloadID);
                        oListItem.set_item('NoOfDownloads', noOfDownloads);
                        oListItem.update();
                        itemArray[i] = oListItem;
                        clientContext.load(itemArray[i]);

                                           }
                }
            }
            clientContext.executeQueryAsync(
                        Function.createDelegate(this, this.onFileUploadSucceeded1),
                        Function.createDelegate(this, this.onFileUploadFailed1)
                    );
        }

Please suggest, what could be the problem.

2 Answers 2

2

Rupesh,

You need to use ProcessBatchData for your scenario if you can use server side code.

If you want to use only CSOM the below code works just fine. I have tested it already

    $(document).ready(function() {    
           ExecuteOrDelayUntilScriptLoaded(abc, "sp.js");
           });

    function abc()
    {
    context = new SP.ClientContext.get_current();
        web = context.get_web();
        var mListName=  "TestList";
        mList = web.get_lists().getByTitle(String(mListName));
          context.load(mList);

    for (var i = 1; i < 250; i++) {
                            var itemCreateInfo = new SP.ListItemCreationInformation();
                            var oListItem = mList.addItem(itemCreateInfo);
                            oListItem.set_item('Title', 'Title' + i.toString());
                            oListItem.update();

                            context.load(oListItem);

                                               }


                context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

    }


function onQuerySucceeded() {

    alert('Items inserted!');
}

function onQueryFailed(sender, args) {

    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

Let me know if you have any questions.

1
  • I want to do by CSOM only :) above code looks same as of mine, except context.load(oListItem); . I am loading thru array because sometimes throws error "oListItem has been changed already". did you check your code for i < 500...? which is an issue for me.
    – Rupesh Rai
    Jul 8, 2015 at 13:53
2

As you are hitting the server to frequently to Add items, SharePoint sometimes do stop the execution in between. I faced the similar issue when i was deleting items from the list. Your code is fine, I would suggest you to add a few moments of delay after adding like 50/100 items. Its even better if you can do an asynchronous loop but it will require you to write new code.

1
  • As I understand, I am not hitting server frequently here. because single async call going thru - executeQueryAsync, which is out of loop body. I can add 500 items if call executeQueryAsync inside loop which will send multiple request to server which I don't want to do. My problem is why works fine if items are 200/250 but not if its 300/400/500.
    – Rupesh Rai
    Jul 8, 2015 at 13:59

Your Answer

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

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