Thursday, January 5, 2012

Delete ALL SharePoint List Items

private void ClearListItems()
{
SPList list = SPContext.Current.Site.RootWeb.Lists["SFA_Bulletin_ConfigValues"];
SPListItemCollection itemCollection;

SPQuery oQuery = new SPQuery();
oQuery.Query = "CAML query here..";

SPContext.Current.Web.AllowUnsafeUpdates = true;


try
{
//SPListItemCollection itemCollection = list.GetItems(oQuery);
itemCollection = list.GetItems(oQuery);
for (var i = itemCollection.Count - 1; i >= 0; i--)
{
try
{
itemCollection[i].Delete();
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}

SPContext.Current.Web.AllowUnsafeUpdates = false;
}

No comments:

Post a Comment