Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Looping through lists and deleting list items; Collection was modified; enumeration operation may not execute

$
0
0

I am trying to loop through a set of lists and clear out old items. I am enumerating the lists but not the items yet it is still failing with Collection was modified; enumeration operation may not execute. However it is failing at foreach SPList - and I have only deleted items from the list and have verified the lists themselves are not getting deleted. 

                                foreach (SPList lstNintexHistory in oWeb.Lists)
                                {
                                    if(lstNintexHistory.Title.ToUpper().Contains("HISTORY"))
                                    {
                                        //Get the Items to Be Deleted
                                        SPListItemCollection licOld = GetOldData(oWeb, lstNintexHistory.Title);
                                        int iLogCount = licOld.Count;
                                        //Delete Items from list
                                        for (int iCount = 0; iCount < iLogCount; iCount++)
                                        {
                                            try
                                            {
                                                licOld.Delete(0);
                                                lstNintexHistory.Update();
                                            }
                                            catch
                                            {

                                            }
                                        }
                                    }
                                }


Viewing all articles
Browse latest Browse all 7589

Trending Articles