错误集合已修改,无法在sharepoint列表上执行枚举

时间:2012-03-06 15:32:15

标签: c# sharepoint-2007 foreach splist

我不知道为什么会出现上述错误,我知道因为UpdateWorkflowAssociation在foreach内部,但我需要这样做 一个简单的帮助将受到高度赞赏

                        `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 个答案:

答案 0 :(得分:1)

而不是

foreach (SPList oList in collList)

简单地写

foreach (SPList oList in collList.ToList())

通过这种方式,您将遍历在迭代期间修改的副本,但真实的集合可以更新。