Deleting items from a SPList programmatically

when enumarating through a splist , and deleting items wihin the enumaration, you’ll get an error telling that there is a problem with teh enumeration. This is because the enumerator no longer knows the enumeration. Since it’s last enumeration the list was changed (ofcourse: an item was deleted).

To avoid this error first initialize an instance of splist in an array , the nstart deleting items using this (static) array.
even easier is to instantiate an splistitemcollection and fill it with items from the list you want to delete items from.

SPSite currentsite = SPcontext.current.site;

SPlist FullList = currentsite.OpenWeb().Lists["name of the list"];

if (FullList.Items.Count > 0 )

{

SPListItemCollection spliCol = FullList.Items;

foreach(SPListItem spli in spliCol)

{

FullList.items[spli.UniqueId].Delete();

}

FullList.Update();

}

2 thoughts on “Deleting items from a SPList programmatically

  1. Thank you for this post!! This is the only post I have seen that actually has a working implementation of deleting more than one item from a Sharepoint list. It really saved me from further frustration!

    Thanks :-)

Leave a Reply

Your email address will not be published. Required fields are marked *