Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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();
        }
}
share|improve this answer
    
Thanks for your input! Very informative! –  Dennis G Jan 11 '11 at 15:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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