SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

Edit

The link that James posted works with the exception of:

parentList.AddWorkflowAssociation

has been deprecated, but:

parentList.WorkflowAssociation.Add 

works

share|improve this question
up vote 3 down vote accepted

This link should provide enough information:

http://blogit.create.pt/blogs/marcosilva/archive/2007/12/10/SharePoint-Associating-a-Workflow-to-a-list.aspx

share|improve this answer
    
@James, thanks for the answer, but this is for 2007, I tried to do it like this but some of the methods have been deprecated. – Shiraz Bhaiji Aug 12 '10 at 7:44

I just gone through this requirement so I am posting the code which I used to attach workflow to List dynamically.

I have written following code in my FeatureActivated event.

SPSite site = properties.Feature.Parent as SPSite;
            SPWeb web = site.RootWeb;
            var test = properties.Feature.Parent as SPSite;
            if (web != null)
            {

                bool allowUnsafeCurrent = web.AllowUnsafeUpdates;
                web.AllowUnsafeUpdates = true;

                SPList list = web.Lists["myList"];
                string workFlowName = "WorkflowName";
                if (list != null)
                {
                    // Get workflow template from workflow name
                    SPWorkflowTemplate baseTemplate = web.WorkflowTemplates.GetTemplateByName
                                                        (workFlowName, CultureInfo.InvariantCulture);

                    if (baseTemplate != null)
                    {
                        SPWorkflowAssociation workflowAssociation =
                            SPWorkflowAssociation.CreateListContentTypeAssociation(
                                baseTemplate,
                                workFlowName,
                                web.Lists["WorkFLowTaskListName"],
                                web.Lists["WorkflowHistoryListName"]);
                        workflowAssociation.AllowManual = true;
                        workflowAssociation.AutoStartChange = false;
                        workflowAssociation.AutoStartCreate = false;
                        list.WorkflowAssociations.Remove(workflowAssociation);
                        list.WorkflowAssociations.Add(workflowAssociation);

                    }
                }
            }

Note : Make sure your feature must be deployed at Site level.

share|improve this answer

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.