title: Sharepoint 2010 - updating large list , processes very slowly and times out
body: <p>I have a situation where I have to update a source records list status column to either open or closed, based on if the records have an open or closed project - the project list can have a number of projects linked to the source record by the ID of the source record and client ID in the projects, the code is to retrieve the clients projects and then check if there is an end date, if there isn't - its open - if there is - its closed. These sharepoint lists are large </p>
  
  <p>I have written code which basically allows me to specify a start ID and then it will process so many records before stopping when I click a run button - I put this in place as the process was painfully slow and was even timing out, but I don't see why its so slow and well .. unstable - if I try process any more than 150 records - it times out, the server itself is an eight core system with 24gb ram so I don't think its the server, Its probably my code.</p>
  
  <pre><code>    protected void Button1_Click(object sender, EventArgs e)
      {
          SPWeb web = SPControl.GetContextWeb(this.Context);
          string SPsiteUrl = SPContext.Current.Web.Url;
          Label1.Text = "Running";
          int start;
          start = Convert.ToInt32(TextBox1.Text);
          start = int.Parse(TextBox1.Text);
          int end = start + 150;
          int count = 0;
  
          using (SPSite oSite = new SPSite(SPsiteUrl))
          {
              using (SPWeb oWeb = oSite.OpenWeb("/Client"))
              {
                  // get lists
                  SPList oList = oWeb.Lists["Source Records"];
                  SPList pList = oWeb.Lists["Project"];
  
                  // query Source Records 
                  string sQuery = @"&lt;Where&gt;&lt;And&gt;&lt;Geq&gt;&lt;FieldRef Name='ID' /&gt;&lt;Value Type='Number'&gt;" + start + "&lt;/Value&gt;&lt;/Geq&gt;&lt;Leq&gt;&lt;FieldRef Name='ID'/&gt;&lt;Value Type='Number'&gt; "+ end +"&lt;/Value&gt;&lt;/Leq&gt;&lt;/And&gt;&lt;/Where&gt;";
                  string sViewFields = @"&lt;FieldRef Name='ID' /&gt;";
                  string sViewAttrs = @"Scope=""Recursive""";
                  uint iRowLimit = 0;
  
                  var oQuery = new SPQuery();
                  oQuery.Query = sQuery;
                  oQuery.ViewFields = sViewFields;
                  oQuery.ViewAttributes = sViewAttrs;
                  oQuery.RowLimit = iRowLimit;
  
                  SPListItemCollection collListItems = oList.GetItems(oQuery);
  
                  // for each item
  
                  foreach (SPListItem oListItem in collListItems)
                  {
                      // get the client id 
  
                      int id = oListItem.ID;
  
                          count = count + 1; 
                          //Label1.Text = "Checking record" + id;
                          bool isopen = false;
                          // for each client id access the projects list
                          // query the closed date
                          string Query = @"&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name=""Client_x003a_ID"" /&gt;&lt;Value Type=""Text""&gt;" + id + "&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;";
                          string pViewFields = @"&lt;FieldRef Name=""End_x0020_Date"" /&gt;";
                          string pViewAttrs = @"Scope=""Recursive""";
                          uint pRowLimit = 0;
                          var pQuery = new SPQuery();
                          pQuery.Query = Query;
                          pQuery.ViewFields = pViewFields;
                          pQuery.ViewAttributes = pViewAttrs;
                          pQuery.RowLimit = pRowLimit;
                          SPListItemCollection ListItems = pList.GetItems(pQuery);
  
                          foreach (SPListItem ListItem in ListItems)
                          {
                              try
                              {
                                  DateTime enddate = (DateTime)ListItem["End_x0020_Date"];
                                  if (enddate != null)
                                  {
                                      isopen = false;
                                  }
                                  // else if the date is null set the open to true
                                  else
                                  {
                                      isopen = true;
                                  }
                              }
                              catch
                              {
                                  string enddate = null;
                                  if (enddate == null)
                                  {
                                      // if the end date is present set project closed to true
                                      isopen = true;
                                  }
                              }
  
                          // if project is open
                          if (isopen)
                          {
                              oWeb.AllowUnsafeUpdates = true;
                              SPListItem itemupdate = oWeb.Lists["Source Records"].Items.GetItemById(id);
                              itemupdate["Status"] = "Open";
                              itemupdate.Update();
                              oWeb.AllowUnsafeUpdates = false;
                              // set records on source record to open
                          }
                          //else // project is closed
                          //{
                          //    oWeb.AllowUnsafeUpdates = true;
                          //    SPListItem itemupdate = oWeb.Lists["Source Records"].Items.GetItemById(id);
                          //    itemupdate["Status"] = "Closed";
                          //    itemupdate.Update();
                          //    oWeb.AllowUnsafeUpdates = false;
                          //    // set the record to closed 
                          //}
                      }
                  }
                  Label1.Text = "Finished Number of records checked: " + count + " of " + collListItems.Count;
              }
          }
      }'
  </code></pre>