0
votes

Can anyone tell me how to create multiple SharePoint list items in batch using CSOM code.

I am referring to the batch creation using CSOM code to similar to the below code.. Below code is in server object model I want to achieve the same in CSOM..

region [ Method : Batch Add List item details ]

    public void AddCanteenMenuDetails(string lstName, List<CanteenMenuDetails> objCanteenMenuDetails)
    {
        StringBuilder sbInsertCanteenMenu = new StringBuilder();
        sbInsertCanteenMenu.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
        using (SPSite site = new SPSite(SPContext.Current.Site.Url))
        {
            using (SPWeb web = site.OpenWeb())
            {
                SPList lstCanteenMenu = web.Lists.TryGetList(lstName);
                bool allowUnsafeUpdates = web.AllowUnsafeUpdates;
                web.AllowUnsafeUpdates = true;
                web.Update();
                foreach (CanteenMenuDetails item in objCanteenMenuDetails)
                {
                    sbInsertCanteenMenu.AppendFormat("<Method ID=\"{0}\">" +
                                            "<SetList>{1}</SetList>" +
                                            "<SetVar Name=\"ID\">New</SetVar>" +
                                            "<SetVar Name=\"Cmd\">Save</SetVar>" +
                                            "<SetVar Name=\"{3}Title\">{2}</SetVar>" +
                                            "<SetVar Name=\"{3}Location\">{4}</SetVar>" +
                                            "<SetVar Name=\"{3}WeekDays\">{5}</SetVar>" +
                     "</Method>", item.Title, lstCanteenMenu.ID, item.Title, "urn:schemas-microsoft-com:office:office#", item.LocationID, item.WeekDays);
                }
                sbInsertCanteenMenu.Append("</Batch>");
                web.ProcessBatchData(sbInsertCanteenMenu.ToString());
                web.AllowUnsafeUpdates = allowUnsafeUpdates;
                web.Update();
            }
        }
    }
    #endregion
1

1 Answers

3
votes

You can try it as below using CSOM C#:

var list = context.Web.Lists.GetByTitle("CustomList");

for (var i = 0; i < 100; i++)
{
    ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
    ListItem oListItem = list.AddItem(itemCreateInfo);

    oListItem["Title"] = "My New Item " + i.ToString();
    oListItem["Description"] = "Test description";
    oListItem.Update();
}                


context.ExecuteQuery();

If you want to use JSOM( the javascript code), you can use it as below:

var itemArray = [];
var clientContext = SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('CustomList');   

for(var i = 0; i< 100; i++){

    var itemCreateInfo = new SP.ListItemCreationInformation();
    var oListItem = oList.addItem(itemCreateInfo);  
    oListItem.set_item('Title', 'New Item ' + i);  
    oListItem.set_item('Description', 'Test desc');  
    oListItem.update();
    itemArray[i] = oListItem;
    clientContext.load(itemArray[i]);
}

clientContext.executeQueryAsync(function(){ 
    console.log("success");
}, function(){
    console.log("error");
});

Please change as per you list names and internal column names.

Reference - Batch operations using javascript

Update - modified as per comments

foreach (CanteenMenuDetails item in objCanteenMenuDetails)
{ 

    ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
    ListItem oListItem = list.AddItem(itemCreateInfo);

    oListItem["Title"] = item.Title;
    oListItem["Location"] = item.LocationID;
    oListItem["WeekDays"] = item.WeekDays;  
    oListItem.Update();

}
context.ExecuteQuery();