Create SharePoint List Items In Batch Using CSOM

Here, we are going to perform the most useful operation which can be performed in SharePoint platform to make our lives easier. We will learn how to create and update list items in batches using CSOM. Using this we can update a huge number of list items in a list. Please follow the below steps to understand the code better.
 
Step 1
 
Set the credential by giving input of user ID and Password for SharePoint.
 
Step 2
 
Using client context get the web & a list in which we want to update the list items.
 
Step 3
 
Using the schema AddFieldAsXML, we can add our custom field as per our requirement. (Here I have created a text field).
 
Step 4
 
Now in this step we will create 20 list items in each field present in the list & update the items using the for loop. We are using for loop for the batch update, only we have to change the condition value of ‘ i ’(e.g. here we used i<20).
 
Now, check the complete code below.
  1. using System;  
  2. using System.Security;  
  3. using Microsoft.SharePoint.Client;  
  4. namespace ConsoleApp7  
  5. {  
  6.    class Program  
  7.    {  
  8.    /// <summary>  
  9.    /// This is the main entry function  
  10.    /// </summary>  
  11.    /// <param name="args"></param>  
  12.    static void Main(string [] args)  
  13.    {  
  14.       #region [Variables]  
  15.       string schemaTextField = string.Empty;  
  16.       string password = string.Empty;  
  17.       #endregion  
  18.       #region [SP Variables]  
  19.       ClientContext ctx = null;  
  20.       Web spWeb = null;  
  21.       List spList = null;  
  22.       SecureString securStr = null;  
  23.       ListItemCreationInformation spItemInfo = null;  
  24.       ListItem spItem = null;  
  25.       Field simpleTextField=null;  
  26.       #endregion  
  27.       using (ctx = new ClientContext("https://contoso.sharepoint.com/sites/sitename"))  
  28.       {  
  29.          try  
  30.          {  
  31.             securStr = new SecureString();  
  32.             password = "Enter the password";  
  33.             foreach (char ch in password)  
  34.             {  
  35.             securStr.AppendChar(ch);  
  36.             }  
  37.             ctx.Credentials = new SharePointOnlineCredentials("Enter UserName", securStr);  
  38.             spWeb = ctx.Web;  
  39.             ctx.Load(spWeb);  
  40.             spList = spWeb.Lists.GetByTitle("listName");  
  41.             schemaTextField = "Field Type='Text' Name='MyCustomTextField' StaticName='MyCustomTextField' DisplayName='MyCustomTextField' />";  
  42.             simpleTextField = spList.Fields.AddFieldAsXml(schemaTextField, true, AddFieldOptions.AddFieldInternalNameHint);  
  43.             ctx.ExecuteQuery();  
  44.             for (var i = 1; i < 20; i++)  
  45.             {  
  46.                try  
  47.                {  
  48.                   spItemInfo = new ListItemCreationInformation();  
  49.                   spItem = spList.AddItem(spItemInfo);  
  50.                   spItem["Title"] = "MyNewItem-" + i.ToString();  
  51.                   spItem["MyCustomTextField"] = "My Custom Values-" + i.ToString();  
  52.                   spItem.Updzcate();  
  53.                }  
  54.                catch (Exception ex)  
  55.                Console.WriteLine("Error:- " + ex.ToString());  
  56.                }  
  57.             }  
  58.             ctx.ExecuteQuery();  
  59.          }  
  60.          catch (Exception ex)  
  61.          {  
  62.             Console.WriteLine("Error:- " + ex.ToString());  
  63.        }  
  64.        }  
  65.      }     
  66.    }  
  67. }   
After running the code, we can go to our respective List and check for the items which are now created successfully.
 
For the output please refer to the below image: