Normally i have the workflow in one wsp, and the list that i am attaching the workflow to in another wsp. I deploy and activate the workflow first, then deploy and activate the list. The list wsp has a feature with event receiver.
public static readonly string Workflow_Name = "Custom Approval";
public static readonly string History_List_Name = "Custom Workflow History";
public static readonly string Tasks_List_Name = "Workflow Tasks";
public static readonly string List_Name = "Custom List";
public override void FeatureActivated(SPFeatureReceiverProperties properties) {
SPWeb web = properties.Feature.Parent as SPWeb;
SPList list = web.Lists[List_Name];
bool historylistfound = false;
foreach (SPList l in web.Lists) {
if (l.Title == History_List_Name) {
historylistfound = true;
}
}
if (!historylistfound) {
SPListTemplateType listtemplate = SPListTemplateType.WorkflowHistory;
web.Lists.Add(History_List_Name, History_List_Name, listtemplate);
}
SPWorkflowAssociation assoc = null;
foreach (SPWorkflowAssociation a in list.WorkflowAssociations) {
if (a.Name == Workflow_Name) {
assoc = a;
}
}
SPList workflowtasks = web.Lists[Tasks_List_Name];
workflowtasks.EnableAssignToEmail = true;
workflowtasks.Update();
if (assoc == null) {
SPWorkflowTemplate template = web.WorkflowTemplates.GetTemplateByName(Workflow_Name, System.Globalization.CultureInfo.CurrentCulture);
assoc = SPWorkflowAssociation.CreateListAssociation(template, Workflow_Name, workflowtasks, web.Lists[History_List_Name]);
assoc.AssociationData = string.Empty;
assoc.AllowManual = true;
assoc.AutoStartChange = false;
assoc.AutoStartCreate = false;
list.AddWorkflowAssociation(assoc);
list.Update();
}
}