Hi this is the sample code for delete the list items at a time using batch
this increase the performance of the delete Query
using following code i deleted list items whose created date is less than the 3 years ago
StringBuilder sb = new StringBuilder();
using (SPSite osite = new SPSite("http://htshydasm38/sites/TestSite/"))
{
using (SPWeb oweb = osite.OpenWeb())
{
try
{
// SPSecurity.RunWithElevatedPrivileges(delegate()
// {
SPList taskList = oweb.Lists["TimerList"];
SPListItemCollection itemCollection = taskList.Items;
int count = 0;
StringBuilder sbDelete = new StringBuilder();
sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
string command = "<Method><SetList Scope=\"Request\">" + taskList.ID
+ "</SetList><SetVar Name=\"ID\">{0}</SetVar>
<SetVar Name=\"Cmd \">Delete</SetVar></Method>";
foreach (SPListItem it in itemCollection)
{
string CeatedDate = Convert.ToString(it["CreatedDate"]);
if (!string.IsNullOrEmpty(CeatedDate))
{
DateTime CreatedDate = Convert.ToDateTime(it["CreatedDate"]);
DateTime CurrentDate = DateTime.Now;
DateTime ThreeYearsoldDate = CurrentDate.AddYears(-3);
sb.Append("Created Date:" + CreatedDate + " ");
sb.Append("CurrentDate:" + CurrentDate + " ");
sb.Append("ThreeYearsold Date:" + ThreeYearsoldDate);
sb.Append("");
if (CreatedDate < ThreeYearsoldDate)
{
count += 1;
sbDelete.Append(string.Format(command, it.ID.ToString()));
}
Label1.Text = "Count" + count + " ---" + sb.ToString();
Label2.Text = sbDelete.ToString();
}
}
sbDelete.Append("</Batch>");
oweb.AllowUnsafeUpdates = true;
if (count > 0)
oweb.ProcessBatchData(sbDelete.ToString());
oweb.AllowUnsafeUpdates = false;
// });
}
catch (Exception ex)
{
Label1.Text = ex.Message.ToString();
}
}
}