0
        using (SPSite siteCollection = new SPSite(HttpContext.Current.Request.Url.ToString()))
        {
            using (SPWeb oWeb = siteCollection.OpenWeb())
            {
                SPListCollection docLibraryColl = oWeb.GetListsOfType(SPBaseType.GenericList);

                foreach (SPList list in docLibraryColl)
                {
                    if (list.Title == "Uygulamalar")
                    {
                        SPQuery oQuery = new SPQuery();
                        oQuery.Query = "<Where><Eq><FieldRef Name='AssignTo'/><Value Type='Integer'><UserID/></Value></Eq></Where>";
                        SPListItemCollection collListItems = list.GetItems(oQuery);

                        foreach (SPListItem item in collListItems)
                        {
                            item.Delete();
                        }

                        foreach (string item in apps)
                        {
                            CheckBox cb = this.FindControl("cb" + item) as CheckBox;
                            if (cb.Checked)
                            {
                                SPListItem oSPListItem = list.Items.Add();
                                oSPListItem["Title"] = item;
                                oSPListItem["AssignTo"] = SPContext.Current.Web.CurrentUser.ID;
                                oSPListItem.Update();
                            }
                        }
                    }
                }
            }
        }
0

I think what you're asking is why you're getting that message. I think it's because you're deleting an item within an enumeration - collListItems - which modifies the enumeration to that it can't, um, enumerate.

Instead, build a list of the things you want to delete, and then work through that:

List<SPListItem> toDelete = new List<SPListItem>();
foreach (SPListItem item in collListItems)
{
    toDelete.Add(item);
}
foreach(SPListItem deleteMe in toDelete) 
{
    deleteMe.Delete();
}

Yes, it does seem long winded.

  • I did use that but still getting same error :/ didn't take from one collection to another. simply nothing changed but collections name? – Mert Apr 16 '15 at 9:40
0

Try:

foreach (SPListItem item in collListItems.ToArray())
{
    item.Delete();
}

Or use a for loop instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.