doing batch items for add/delete/modify list on sharepoint online?

nellie 126 Reputation points
2022-07-11T19:33:51.42+00:00

Have examples of this in c# please?

Microsoft 365 and Office | SharePoint | For business | Windows
Developer technologies | C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2022-07-11T22:18:06.577+00:00
    0 comments No comments

  2. Tong Zhang_MSFT 9,251 Reputation points
    2022-07-12T07:09:53.717+00:00

    Hi @nellie ,

    According to my research and testing, you can use the following code to create/update/delete list items in batch using CSOM :

                var ctx = GetonlineContext();  
                Web spWeb = ctx.Web;  
                spWeb = ctx.Web;  
                ctx.Load(spWeb);  
                List spList = spWeb.Lists.GetByTitle("<ListName>");  
      
                //Create SharePoint List Items In Batch  
                for (var i = 1; i < 20; i++)  
                {  
                    try  
                    {  
                        ListItemCreationInformation spItemInfo = new ListItemCreationInformation();  
                        ListItem spItem = spList.AddItem(spItemInfo);  
                        spItem["Title"] = "MyNewItem-" + i.ToString();  
                        spItem.Update();  
                    }  
                    catch (Exception ex) {   
                   Console.WriteLine("Error:- " + ex.ToString());  
                    }  
                }  
                ctx.ExecuteQuery();  
      
      
                //update SharePoint List Items In Batch  
                /*  
                for (var i = 1; i < 20; i++)  
                {  
                    try  
                    {  
                        ListItem spItem = spList.GetItemById(i);  
                        spItem["Title"] = "UpdatedItem-"+i.ToString();  
                        spItem.Update();  
                    }  
                    catch (Exception ex)  
                    {  
                        Console.WriteLine("Error:- " + ex.ToString());  
                    }  
                }  
                ctx.ExecuteQuery();  
                */  
      
      
                //delete SharePoint List Items In Batch  
                /*  
                for (var i = 1; i < 10; i++)  
                {  
                    try  
                    {  
                        ListItem spItem = spList.GetItemById(i);  
                        spItem.DeleteObject();  
                    }  
                    catch (Exception ex)  
                    {  
                        Console.WriteLine("Error:- " + ex.ToString());  
                    }  
                }  
                ctx.ExecuteQuery();  
                */  
    

    More information for reference:
    https://www.c-sharpcorner.com/blogs/create-sharepoint-list-items-in-batch-using-csom2

    Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.