Friday, February 4, 2011

Insert Bulk data in Sharepoint

Question: How to insert the bulk data(near about 1000 and more records) in sharepoint list.?
Answer : I tried to insert the data in sharepoint list. And this data is near about 1000 records and above.
Here we go to insert using "XML BATCH PROCESS"
I belive me that this insertion takes only few seconds. yes only few seconds
here is the sample code.
try
      {
        StringBuilder methodBuilder = new StringBuilder();
        string batch = string.Empty;
        DateTime currentDate = DateTime.Now;
        string formattedDate = DateTime.Now.ToString();

        string methodFormat = string.Empty;

        SPSite site = new SPSite("http://localhost");

        SPWeb CurrentSite = SPContext.Current.Web ;         

            // Get the list containing the items to update.           
            SPList list = CurrentSite.Lists["TempList"];
            string listGuid = list.ID.ToString();
                                 
             for (int i = 0; i < 1000; i++)
             { 
               int itemID = i;
              
               string strPost = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "<ows:Batch OnError=\"Return\">" +
                  "<Method ID=\"A1\"><SetList>" + listGuid + "</SetList>" +
                    "<SetVar Name=\"ID\">New</SetVar>" +
                    "<SetVar Name=\"Cmd\">Save</SetVar>" +
                    "<SetVar Name=" + "\"urn:schemas-microsoft-com:office:office#Title\">" + "New</SetVar>" +
                    "<SetVar Name=" + "\"urn:schemas-microsoft-com:office:office#EnrollmentType\">" + "Normal </SetVar>" +
                    "<SetVar Name=" + "\"urn:schemas-microsoft-com:office:office#EmployeeName\">" + CurrentSite.CurrentUser.Name.ToString()+"</SetVar>" +
                    "<SetVar Name=" + "\"urn:schemas-microsoft-com:office:office#EmployeeNumber\">" + CurrentSite.CurrentUser.ID.ToString() +"</SetVar>" +
                    "<SetVar Name=" + "\"urn:schemas-microsoft-com:office:office#EmployeeAccount\">" + CurrentSite.CurrentUser.LoginName.ToString() + "</SetVar>" +
                    "<SetVar Name=" + "\"urn:schemas-microsoft-com:office:office#PhoneNo\">123456</SetVar>" +
                    "<SetVar Name=" + "\"urn:schemas-microsoft-com:office:office#AssignmentType\">Individual</SetVar>" +
                    "<SetVar Name=" + "\"urn:schemas-microsoft-com:office:office#Flag\">N</SetVar>" +
                    "<SetVar Name=" + "\"urn:schemas-microsoft-com:office:office#Status\">Assigned/RFE Not Sent</SetVar>" +
                    "<SetVar Name=" + "\"urn:schemas-microsoft-com:office:office#AssignmentID\">" + "ASN" + i + "</SetVar>" +
                  "</Method>"+"</ows:Batch>";
               string strProcessBatch = SPContext.Current.Web.ProcessBatchData(strPost);

             }
       
      } 
      catch (Exception ex) {
        inputName.Text = ex.Message;
      }

No comments:

Post a Comment