错误集合已修改,无法对共享点列表执行枚举

本文关键字:共享 列表 执行 枚举 集合 修改 错误 | 更新日期: 2023-09-27 18:26:48

不确定我为什么会出现上述错误,我知道这是因为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");
                            }
                        }`

错误集合已修改,无法对共享点列表执行枚举

而不是

foreach (SPList oList in collList)

只需写入

foreach (SPList oList in collList.ToList())

这样,您将迭代一个在迭代过程中未被修改的副本,但可以更新真实的集合。