Tags

,

In general my ideas for my posts are generated by problems I am facing in my development activity. One of the problems working JavaScript CSOM is sometimes projects require very complex data operations, like adding items into multiple SharePoint lists. I would not call this a challenge, as you always can use classical examples and perform insert operations one by one, waiting for previous operation to be completed before starting a new one. But why not make it easier?

If you want to achieve this in a simple way, just take a look on the code below.

Type.registerNamespace('Shp');


Shp.Lists.AddItemsInLists = function (addItemsArguments, webUrl, success, fail) {
    /// <summary>Add items in multiple lists calling execute query async once</summary>
    /// <param name="addItemsArguments" type="Array" elementType="Object" elementMayBeNull="false" mayBeNull="false" optional="false">Add items arguments</param>
    /// <param name="webUrl" type="String" mayBeNull="true" optional="false">Web url</param>
    /// <param name="success" type="Function" mayBeNull="false" optional="false">Success</param>
    /// <param name="fail" type="Function" mayBeNull="false" optional="true">Fail</param>
    var e = Function.validateParameters(arguments, [{ name: 'addItemsArguments', type: Array, elementType: Object, elementMayBeNull: false, optional: false, mayBeNull: false },
                                                    { name: 'webUrl', type: String, optional: false, mayBeNull: true },
                                                    { name: 'success', type: Function, optional: false, mayBeNull: false },
                                                    { name: 'fail', type: Function, optional: true, mayBeNull: false }], true);
    if (e) throw e;
    var ctx = (typeof webUrl === 'undefined' || webUrl === null) ? SP.ClientContext.get_current() : new SP.ClientContext(webUrl);
    var fail = fail || function (err) { alert(err); };
    Shp.Lists._AddItemsInLists(addItemsArguments, ctx, success, fail);
};


Shp.Lists._AddItemsInLists = function (addItemsArguments, ctx, success, fail) {
    var results = {}, current, i, k;

    for (i = 0; i < addItemsArguments.length; i++) {

        current = addItemsArguments[i]; // Store current item

        // We check if list name property if defined before do other operations
        if (current.hasOwnProperty("listName") === true) {

            if (results.hasOwnProperty(current.listName) === false) results[current.listName] = []; // Results object doesn't have a slot for current list name, create it
            var oList = ctx.get_web().get_lists().getByTitle(current.listName);

            // We check if list items property is an array
            if (current.hasOwnProperty("listItems") === true && isArray(current.listItems) === true) {
                for (k = 0; k < current.listItems.length; k++) {
                    var listItem = current.listItems[k];
                    var oListItem = oList.addItem(new SP.ListItemCreationInformation());
                    for (var field in listItem) {
                        oListItem.set_item(field, listItem[field]);
                    }
                    oListItem.update();
                    ctx.load(oListItem);
                    results[current.listName].push(oListItem);
                }
            }
        }
    }

    ctx.executeQueryAsync(function () {
        success(results);
    }, function (sender, args) {
        fail(args.get_message());
    });

};

Shp.Lists.registerClass('Shp.Lists');

I have created a method “AddItemsInLists” for “Shp.Lists”, which is accepting 4 parameters:

  • addItemsArguments – which is an array of objects containing information about list items you want to insert.
  • webUrl – which is the URL of the web inside current site collection. If null, web for current context is considered.
  • success – function to be executed if everything goes fine.
  • fail – function to be executed if fails. It is optional and if not specified a simple alert containing the error is shown.

Let’s concentrate on “addItemsArguments” parameter. As I said, it is an array of objects, but let me explain how these objects should look like. Basically it should contain 2 properties: “listName” (name of the list where you want to insert items) and “listItems” (another array of objects, this time representing the items).

var firstListItems = { listName: 'Name of the list', listItems: [{ 'Field 1': 'Value', 'Field 2': 'Value' }, { 'Field 1': 'Value', 'Field 2': 'Value'}]  };
var secondListItems = { listName: 'Name of the 2nd list', listItems: [{ 'Field 1': 'Value', 'Field 2': 'Value' }, { 'Field 1': 'Value', 'Field 2': 'Value'}]  };
var addItemsArguments = [firstListItems, secondListItems];

Now calling the method I have created is easy and no rocket science is required.

Shp.Lists.AddItemsInLists([firstListItems, secondListItems, ], null, function (results) {
    /*  Do something with the results.
        You can get items inserted in a list like this: results['name of the list']  */
}, function (err) {
    alert(err); // Alert the error or do something elsel
});
Advertisements