Add SharePoint workflow to a list programmatically

sharepointsharepoint-2010workflow

We are using SharePoint Foundation 2010.

We have created a workflow that checks the status of a task list.

If we associate the workflow with the list in the SharePoint UI it works fine.

We were wondering how we could automatically associate it, maybe in the feature receiver code that sets ut the site?

Best Answer

// 1. create an instance of the SPWorkflowAssociation class
SPWorkflowAssociation workflowAssociation =
  SPWorkflowAssociation.CreateListAssociation(workflowTemplate, associationName, taskList, historyList);

// 2. set start options
workflowAssociation.AllowManual = true;
workflowAssociation.AutoStartChange = false;
workflowAssociation.AutoStartCreate = false;

// 3. set additional association options (if any)
workflowAssociation.AssociationData = associationData;

// 4. add workflow association to the list
list.WorkflowAssociations.Add(workflowAssociation);

// 5. enable workflow association, so it is displayed in the user interface
workflowAssociation.Enabled = true;
Related Topic