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);
}
}
}
}
}