Create SharePoint list items in batch using CSOM
Softree Technology > Blogs > SharePoint > Create SharePoint list items in batch using CSOM

Create SharePoint list items in batch using CSOM

  • Posted by: Softree Technology

Here, we are going to perform the most useful operation which can be performed in SharePoint platform to make our commercial life easier. We will learn how to create and update list items in batch 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 foe 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.

using System;
using System.Security;
using Microsoft.SharePoint.Client;

namespace ConsoleApp7
{
    class Program
    {
        /// <summary>
        /// This is the main entry function
        /// </summary>
        /// <param name="args"></param>
        static void Main(string [] args)
        {
            #region [Variables]
            string schemaTextField = string.Empty;
            string password = string.Empty;
            #endregion #region [SP Variables]
            ClientContext ctx = null;
            Web spWeb = null;
            List spList = null;
            SecureString securStr = null;
            ListItemCreationInformation spItemInfo = null;
            ListItem spItem = null;
            Field simpleTextField=null;
            #endregion

            using (ctx = new ClientContext("https://contoso.sharepoint.com/sites/sitename"))
            {
              try
              {
                securStr = new SecureString();
                password = "Enter the password";
                foreach (char ch in password)
                {
                    securStr.AppendChar(ch);
                }
                ctx.Credentials = new SharePointOnlineCredentials("Enter UserName", securStr);
                spWeb = ctx.Web;
                ctx.Load(spWeb);
                spList = spWeb.Lists.GetByTitle("listName");
                schemaTextField = "<Field Type='Text' Name='MyCustomTextField' StaticName='MyCustomTextField' DisplayName='MyCustomTextField' />";
                simpleTextField = spList.Fields.AddFieldAsXml(schemaTextField, true, AddFieldOptions.AddFieldInternalNameHint);
                ctx.ExecuteQuery();
                for (var i = 1; i < 20; i++)
                {
                    try
                    {
                        spItemInfo = new ListItemCreationInformation();
                        spItem = spList.AddItem(spItemInfo);
                        spItem["Title"] = "MyNewItem-" + i.ToString();
                        spItem["MyCustomTextField"] = "My Custom Values-" + i.ToString();
                        spItem.Update();
                    }
                    catch (Exception ex)
                    Console.WriteLine("Error:- " + ex.ToString());
                    }
                }
                ctx.ExecuteQuery();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error:- " + ex.ToString());
            }
           }
       }
    }
} 

After running the code, we can go to our respective List and check for the items which are now created successfully.

For output please refer below image:                                                                         

Keywords:

  • Create SharePoint list items in batch using CSOM
  • Creating List item in batch using CSOM
  • How to create bulk list Item using CSOM
  • Batch Update in CSOM
  • Bulk Update using CSOM
  • Bulk update in SharePoint
  • Batch Update in SharePoint
Author: Softree Technology

Leave a Reply