Home > SharePoint 2010 > Programmatically Associating a Workflow to a Site

Programmatically Associating a Workflow to a Site

When you deploy a site-level workflow using Visual Studio 2010, Post-Deployment build configuration takes care of activating the workflow feature, and programmatically associates the workflow to the site.

When you deploy the wsp package using PowerShell or stsadm , the magic won’t happen! So, if you go to View All Site Content > Site Workflows, the page is pretty empty with a message saying that “there are no workflows currently available to start on this site”

noworkflow.PNG

What you need to do is to put the following code in your feature receiver:

[CSharp]
private string workflowTemplateBaseGuid = “1e547b46-3d00-43b0-928d-4fe103b92fcb”;
private string workflowAssociationName = “My Workflow”;
private string workFlowHistoryListName = “Workflow History”;
private string workFlowTaskListName = “Workflow Tasks”;

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
var site = properties.Feature.Parent as SPSite;
var web = site.RootWeb;
SPWorkflowTemplateCollection workflowTemplates = web.WorkflowTemplates;
SPWorkflowTemplate workflowTemplate =
workflowTemplates.GetTemplateByBaseID(new Guid(workflowTemplateBaseGuid));

if (workflowTemplate != null)
{
// Create the workflow association
SPList taskList = web.Lists[workFlowTaskListName];
SPList historyList = web.Lists[workFlowHistoryListName];
SPWorkflowAssociation workflowAssociation =
web.WorkflowAssociations.GetAssociationByName
(workflowAssociationName, CultureInfo.InvariantCulture);

if(workflowAssociation ==null)
{
workflowAssociation = SPWorkflowAssociation.CreateWebAssociation
(workflowTemplate, workflowAssociationName, taskList, historyList);
workflowAssociation.AllowManual = true;
workflowAssociation.Enabled = true;
web.WorkflowAssociations.Add(workflowAssociation);
}
}
}
[/CSharp]

One thing to note is that in sharePoint 2010, SPSite.SPWorkflowManager.GetWorkflowTemplatesByCategory() doesn’t properly return the workflow template collection like SharePoint 2007. So, I had to fall back to the traditional way of using SPWeb.WorkflowTemplates to get my workflow template and create the SPWorkflowAssociation object.

Obviously, in your FeatureDeactivating code, you need to add the necessary code to remove the current instances of the workflow; otherwise if you deactivate the feature and activate it, the workflow doesn’t show up again in View All Site Content> Site Workflows again. A no-code workaround for this issue is to go to Site Settings > Site Administration > Workflow Settings; and remove the instance manually.

Categories: SharePoint 2010 Tags:
  1. Andre
    October 14th, 2011 at 06:01 | #1

    Hey Reza,

    you save my day.
    thank you!

    Best Rergards Andre

  2. October 27th, 2011 at 20:11 | #2

    Hi,

    I was getting an Exception follwing your method, where SPWeb was returning null on the internal call to …Enabled = True.
    had to make this small change to make it work for me:

    workflowAssociation = web.WorkflowAssociations.Add( SPWorkflowAssociation.CreateWebAssociation (workflowTemplate, workflowAssociationName, taskList, historyList));

    workflowAssociation.AllowManual = true;
    workflowAssociation.Enabled = true;

    It was a good help.
    Thanks for sharing!

  3. March 2nd, 2012 at 12:02 | #3

    Thank you Jorge, this was driving me nuts.

  4. Fabian
    June 4th, 2012 at 07:41 | #4

    Hi Reza,

    I really thank you for your post, you saved my day! I didn’t know where the problem was with the Method GetWorkflowTemplateByCategory and either found some information on the web. But your alternative way with web.WorkflowTemplates made my feature receiver work!

    Thank you soo much!

    • Reza Alirezaei
      June 22nd, 2012 at 08:46 | #5

      @Fabian Glad I could help

  5. Rich
    October 16th, 2013 at 09:39 | #6

    Hi Reza,

    The behavior you are writing about here started happening to me but only after I moved the workflow into Team Foundation Server 2010. Prior to this I could automatically activate and associate the workflow to my list from a Visual Studio deployment or from a powershell script. Now, after adding to TFS, I am not able to associate it to the list using either method! I can activate it via Powershell but it never associates to the list. Have you seen this behavior before..Does that make any sense??

You must be logged in to post a comment.