Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all rows in sharepoint list using Client Context and CAML Query

I am new to SharePoint and want to delete all rows in a SharePoint list using C# ClientContext class and CAML Query.

How can i achieve it efficiently?

like image 306
Yogesh Avatar asked Apr 15 '13 11:04

Yogesh


1 Answers

I solved it. The learning was that we need to delete the items of list in reverse order.

Link: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitemcollection.delete.aspx

ListItemCollection listItems = oList.GetItems(CamlQuery.CreateAllItemsQuery());
clientContext.Load(listItems,
                    eachItem => eachItem.Include(
                    item => item,
                    item => item["ID"]));
clientContext.ExecuteQuery();

var totalListItems = listItems.Count;
Console.WriteLine("Deletion in " + currentListName + "list:");
if (totalListItems > 0)
{
    for (var counter = totalListItems - 1; counter > -1; counter--)
    {
        listItems[counter].DeleteObject();
        clientContext.ExecuteQuery();
        Console.WriteLine("Row: " + counter + " Item Deleted");
    }
}
like image 178
Yogesh Avatar answered Sep 17 '22 17:09

Yogesh