In this post we will discuss about how to associate a workflow to a list programmatically in SharePoint 2010. Also you can check my previous posts on:
Below is the full code:
using(SPSite site = new SPSite("Site URL"))
{
SPWeb web = site.OpenWeb();
SPList list = web.Lists["List Name"];
SPWorkflowTemplate workflowTemplate = web.WorkflowTemplates.GetTemplateByName
("NameOfWorkflowTemplate", System.Globalization.CultureInfo.CurrentCulture);
SPList workflowTaskList = web.Lists["Tasks"];
SPList workflowHistoryList = web.Lists["Workflow History"];
SPWorkflowAssociation wfAssociation = SPWorkflowAssociation.CreateListAssociation
(workflowTemplate, "Give Workflow Name", workflowTaskList, workflowHistoryList);
wfAssociation.AutoStartChange = false;
wfAssociation.AutoStartCreate = false;
wfAssociation.AllowManual = true;
web.AllowUnsafeUpdates = true;
list.AddWorkflowAssociation(wfAssociation);
list.Update();
web.AllowUnsafeUpdates = false;
}