I have an event receiver that is looping through a group of lists and associating a workflow to it if they have content approval enabled. The feature fails to activate with this error:
Error occurred in deployment step ‘Activate Features’: Collection was modified; enumeration operation may not execute.
Here is the code snippet. If I comment out the line in bold, it activates without error:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
   using (SPSite site = properties.Feature.Parent as SPSite)
   {
     using (SPWeb web = site.OpenWeb(“/marketingdc”))
     {
        SPWorkflowTemplate wfTemplate = web.WorkflowTemplates.GetTemplateByName(“CPMDApproval”, web.Locale);
        SPList tasks = web.Lists[“Tasks”];
        SPList wfHistory = web.Lists[“Workflow History”];
        foreach (SPList list in web.Lists.Cast<SPList>().Where(l => l.EnableModeration == true))
        {
          SPWorkflowAssociation wfAssociation = list.WorkflowAssociations.GetAssociationByName(“Marketing Docs Approval”, web.Locale);
          if (wfAssociation == null)
          {
             wfAssociation = SPWorkflowAssociation.CreateListAssociation(wfTemplate, “Marketing Docs Approval”, tasks, wfHistory);
             wfAssociation.AllowManual = true;
             wfAssociation.AutoStartChange = false;
             wfAssociation.AutoStartCreate = false;
             list.WorkflowAssociations.Add(wfAssociation);
          }
          else
          {
             wfAssociation.AllowManual = true;
             wfAssociation.AutoStartChange = false;
             wfAssociation.AutoStartCreate = false;
             list.WorkflowAssociations.Update(wfAssociation);
          }
        }
     }
   }
}
Paul my hunch is that something is modifying your “web.Lists” collection within the loop.Â
Can you try this (not tested)
Nice :Â
and for copy and paste :
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
List<Guid> listOfLists = new List<Guid>();
using (SPSite site = properties.Feature.Parent as SPSite)
{
using (SPWeb web = site.OpenWeb(“/marketingdc”))
{
// get the lists now.
foreach (SPList list in web.Lists.Cast<SPList>().Where(l => l.EnableModeration == true))
{
listOfLists.Add(list.ID);
}
SPWorkflowTemplate wfTemplate = web.WorkflowTemplates.GetTemplateByName(“CPMDApproval”, web.Locale);
SPList tasks = web.Lists[“Tasks”];
SPList wfHistory = web.Lists[“Workflow History”];
// now you want modify the web.Lists collection.
foreach (Guid listGuid in listOfLists)
{
SPList list = web.Lists[listGuid];
SPWorkflowAssociation wfAssociation = list.WorkflowAssociations.GetAssociationByName(“Marketing Docs Approval”, web.Locale);
if (wfAssociation == null)
{
wfAssociation = SPWorkflowAssociation.CreateListAssociation(wfTemplate, “Marketing Docs Approval”, tasks, wfHistory);
wfAssociation.AllowManual = true;
wfAssociation.AutoStartChange = false;
wfAssociation.AutoStartCreate = false;
list.WorkflowAssociations.Add(wfAssociation);
}
else
{
wfAssociation.AllowManual = true;
wfAssociation.AutoStartChange = false;
wfAssociation.AutoStartCreate = false;
list.WorkflowAssociations.Update(wfAssociation);
}
}
}
}
}
?width=721