How do you deploy your custom Visual Studio Workflows to automatically create a WorkflowHistory and task list (Creating a List programmatically using the object model)?

I know I can first deploy the Workflow and afterwards run a Powershell/Console script to create my lists and associate the Workflow with these - but is there a better solution?

I'm asking because Visual Studio can associate the Workflow with a list on deployment, but only with existing lists so I would have to have the lists created beforehand.

1 Answer 1


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;

        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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.