How to add multiple items into a SharePoint list at one transaction?


  • you can submit commands in batch using ProcessBatchData API on SPWeb, basically you just generate a CAML that will contain all the commands you want to run in batch.
    A Collaborative Application Markup Language (CAML) that contains the commands, which consists of a Batch element and any number of subordinate Method elements that each specify a SharePoint Foundation remote procedure call (RPC) method.
    To use this method to delete a document in a Document Library, pass the file path to the owsfileref variable in the Method elements.
    The following code example uses the ProcessBatchData method to add two items to the Announcements list of a specified site in the current site collection.

    using (SPWeb oWebsite = SPContext.Current.Site.OpenWeb("Website_URL"))
    {
        SPList oList = oWebsite.Lists["Announcements"];
        System.Guid guid = oList.ID;
        string strGuid = guid.ToString();
    
        string strPost = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
            "<ows:Batch OnError=\"Return\">" +
                "<Method ID=\"A1\"><SetList>" + strGuid + "</SetList>" +
                    "<SetVar Name=\"ID\">New</SetVar>" +
                    "<SetVar Name=\"Cmd\">Save</SetVar>" +
                    "<SetVar Name=" +
                        "\"urn:schemas-microsoft-com:office:office#Title\">" +
                        "New Manager</SetVar>" +
                    "<SetVar Name=" +
                        "\"urn:schemas-microsoft-com:office:office#Body\">" +
                        "Congratulations to Mary for her promotion!</SetVar>" +
                    "<SetVar Name=" +
                        "\"urn:schemas-microsoft-com:office:office#Expires\">" +
                        "2003-09-14T00:00:00Z</SetVar>" + 
                "</Method>" +
                "<Method ID=\"A2\">" +
                    "<SetList>" + strGuid + "</SetList>" +
                    "<SetVar Name=\"ID\">New</SetVar>" +
                    "<SetVar Name=\"Cmd\">Save</SetVar>" +
                    "<SetVar Name=" +
                        "\"urn:schemas-microsoft-com:office:office#Title\">" +
                        "New Technical Consultant</SetVar>" +
                    "<SetVar Name=" +
                        "\"urn:schemas-microsoft-com:office:office#Body\">" +
                        "Welcome to the team, John!</SetVar>" +
                    "<SetVar Name=" +
                        "\"urn:schemas-microsoft-com:office:office#Expires\">" +
                        "2007-10-15T00:00:00Z</SetVar>" + 
                "</Method>" +
            "</ows:Batch>";
    
        string strProcessBatch = oWebsite.ProcessBatchData(strPost);
    }