Delete all items from a SPListItemCollection


Hi Folks.Today my friend asked me a question on how to delete all the items of a list item collection.
I told him to loop through all the items and delete it using the index.But it resulted in an exception.
Then i suggested him to write a negative loop.That worked! 
But me and my friend (Deba) found it rather funny that by default there is not simple way to delete all the items.
Well here is the method which we used.


protected void DeleteAllItemsInCollection(SPListItemCollection collListItems)
{
//Deletes all the items of list item collection
for (int intIndex = collListItems.Count - 1; intIndex > -1; intIndex--)
{
collListItems.Delete(intIndex);
}
}


Reference : 
1. MSDN Reference Link
2. Another Good Reference for the same topic


Cheers

Comments

Post a Comment

Popular posts from this blog

Install Node.js without admin rights

Create a lean React Solution using Typescript

Replace all occurence of String in JavaScript