Add more items to a list in the same request

Again I bring into discussion JavaScript CSOM. I am not doing this just for fun, even I recognize SharePoint development can be fun. I am doing this because is probably the recommended way to add list items using CSOM, as more and more companies are migrating to Office 365, where no server side development is allowed on SharePoint Online.

You can find how to do this on Microsoft website, but my article is about something else. Is about how to have your code organized and how you can even insert more items in the same request. So, let’s start!

/// <Reference name="MicrosoftAjax.js" />


Shp.List = function (listName, webUrl) {
    /// <summary>Create an instance of Shp.Lists class</summary>
    /// <param name="listName" type="String" optional="false" mayBeNull="false">List name</summary>
    /// <param name="webUrl" type="String" optional="true" mayBeNull="false">Web URL</summary>
    var e = Function.validateParameters(arguments, [{ name: "listName", type: String, optional: false, mayBeNull: false },
                                                    { name: "webUrl", type: String, mayBeNull: false, optional: true }], true);
    if (e) throw e;

    this.listName = listName;
    this.ctx = (typeof (webUrl) === 'undefined') ? SP.ClientContext.get_current() : new SP.ClientContext(webUrl);
    this.oWeb = this.ctx.get_web();
    this.oList = this.oWeb.get_lists().getByTitle(this.listName);
    return this;

Shp.List.prototype = {

    add_items: function (listItems) {
        /// <summary>Add list items to the list</summary>
        /// <param name="listItems" type="Array" mayBeNull="false" optional="false" elementType="Object" elementMayBeNull="false">List items</param>
        var e = Function.validateParameters(arguments, [{ name: "listItems", type: Array, mayBeNull: false, optional: false, elementType: Object, elementMayBeNull: false }], true);
        if (e) throw e;

        var instance = this;
        var defferred = jQuery.Deferred();
        var results = [];

        for (var k = 0; k < listItems.length; k++) {
            var itemCreateInfo = new SP.ListItemCreationInformation();
            var oListItem = instance.oList.addItem(itemCreateInfo);
            // Set fields values
            var listItem = listItems[k];
            for (var field in listItem) {
                oListItem.set_item(field, listItem[field]);
       [ }

        instance.ctx.executeQueryAsync(function () {
        }, function (sender, args) {

        return defferred.promise();


I have created above an Shp.List object which can be initialized with two parameters. First one is the list name, which is mandatory, and the second one is web URL, which is optional. If you need to do your operation on a list in a sub-site, for example, please specify the URL of it. If you don’t specify the URL, client context will be created for current web.

Shp.List object was a method called “add_items”, which takes an array of objects as parameter and return a jQuery Deffered object. Let’s see how it can be used.

    var incident = {};
    incident['Title'] = 'Title 1';
    incident['Priority'] ='Priority 1';
    incident['TicketType'] ='Ticket type 1';

    var incident2 = {};
    incident2['Title'] = 'Title 2';
    incident2['Priority'] ='Priority 2';
    incident2['TicketType'] ='Ticket type 2';

    var items = new Shp.List("Tickets").add_items([incident, incident2]); (err) {

    items.done(function (items) {

        // Get the id of created items
        for (var i = 0; i < items.length; i++) {


Please note the property names for objects are internal names of the fields.

The next challenge for me is to create a way to insert a very big number of items calling the method once. Now it is working with a small number of items, but, if number of items is too big, you might receive “Request uses too many resources” error.

Leave a Reply

Your email address will not be published. Required fields are marked *