[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 342
  • Last Modified:

Sharepoint Workflows

There is a custom workflow (Sequential workflow creatind vs2010) running on a list, the
workflow is having sendmail activity once the mail is sent i want to remove items from the list

code for removing/deleting items in list has been placed in "Code Activity" but i am getting an error "Collection was modified; enumeration operation may not execute."

what is the better way to overcome this problem

Code
        private void RemoveItems()
        {
           
            using (SPSite site = new SPSite("http://xxx"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    try
                    {
                        SPList list = web.Lists["aa"];

                        foreach (SPListItem item in list.Items)
                        {
                            item.Delete();
                        }

                    }
                    catch (Exception ex)
                    {

                    }
                }
            }

        }
0
msdevguy
Asked:
msdevguy
1 Solution
 
GeorgeGerguesCommented:
I am not exactly sure how / what is going wrong , as this is code could be very specific to your list / library
but here is what I see wrong .

You must update the list on every change  ,specillay with a hight frequencey change like yours.

So here is the change
Code
        private void RemoveItems()
        {
            
            using (SPSite site = new SPSite("http://xxx"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    try
                    {
                        SPList list = web.Lists["aa"];

                        foreach (SPListItem item in list.Items)
                        {
                            item.Delete();
                            list.Update();

                        }

                    }
                    catch (Exception ex)
                    {

                    }
                }
            }

        }

Open in new window

0
 
msdevguyAuthor Commented:
that did not worked exactly i have used a different way of deleting it
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now