2

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

3

This link should provide enough information:

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

  • @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
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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