0

not sure why i get the above error, i know its because of UpdateWorkflowAssociation is inside the foreach but i need it that way A simple help will be highly appreciated

                        `siteName = "http://xyz";

                        newCleanupDays = 5;
                        assoCounter = 0;
                        using (wfSite = new SPSite(siteName))
                        {
                            using (wfWeb = wfSite.OpenWeb())
                            {

                               //wfList = wfWeb.Lists[libraryName];
                                SPListCollection collList = wfWeb.Lists; //Open Lists
                                SPWorkflowAssociation _wfAssociation = null;

                                foreach (SPList oList in collList)
                                {
                                    if (oList.WorkflowAssociations.Count > 0)
                                    {
                                        foreach (SPWorkflowAssociation a in oList.WorkflowAssociations)
                                        {
                                            if (a.Name != null || a.Name != string.Empty)
                                            {
                                                a.AutoCleanupDays = newCleanupDays;
                                                _wfAssociation = a;
                                                assoCounter++;
                                            }
                                            else
                                            {
                                                _wfAssociation = a;
                                            }

                                        }
                                        oList.UpdateWorkflowAssociation(_wfAssociation);
                                    }

                                }
                                System.Console.WriteLine("\n" + wfAssoName + ": " + assoCounter.ToString() + " workflow association(s) changed successfuly!\n");
                            }
                        }`

1 Answers1

1

Instead of

foreach (SPList oList in collList)

simply write

foreach (SPList oList in collList.ToList())

That way you will iterate over a copy which is not modified during the iteration, but the real collection can be updated.

Timwi
  • 65,159
  • 33
  • 165
  • 230
  • collList is SPlistcollection it does not have ToList() method – mainu raj kehte hai Mar 06 '12 at 16:27
  • @mainurajkehtehai: It doesn’t need one. It’s an extension method on `IEnumerable`. Just add `using System.Linq;` at the top. (If `SPlistcollection` doesn’t implement `IEnumerable`, consider implementing it.) – Timwi Mar 06 '12 at 17:22
  • I fixed it by the below code as @Timwi said created a copy and iterated that list for modification. @Timwi: i would like know more on IEnumerable ..you know some kind of study guide Thanks `List itemsEnum = new List(); foreach (SPList oList in collList) { if (oList.WorkflowAssociations.Count > 0) { itemsEnum.Add(oList); } } foreach (SPList var in itemsEnum) {` – mainu raj kehte hai Mar 07 '12 at 11:25
  • After iterating through copy list these code although does update original list "oList" not sure why ? cannu pls help – mainu raj kehte hai Mar 19 '12 at 05:49
  • Your sentence does not make sense. Sorry. – Timwi Mar 19 '12 at 17:29
  • Not an issue actually The workflows on which i tested these code were in error state so i thought the above code is not working ....but it is working ...thanks for your reply. I Appreciate thse – mainu raj kehte hai Mar 20 '12 at 15:19