Friday, 17 January 2014

Attach Workflow Programatically

So you have a workflow and you want to associated to a list, but every time you deploy your project, you have to go back and re-add the thing to your list which is tedious. well this is the position I've currently found myself in, to rectify it I'm adding an event receiver and am going to associate the workflow with the list on feature activating.

right click on your feature, and click add feature

mine's grey out because I've already done it, but yours should be selectable.

You're going to need your Workflow's ID, to get this open the Workflow's  Elements.xml file.

with the Elements file, grab the GUID of the Id attribute of the Workflow Element.

<?xml version="1.0" encoding="utf-8" ?>

<!-- Customize the text in square brackets.
Remove brackets when filling in, e.g.
Name="[NAME]" ==> Name="MyWorkflow" -->

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Workflow
     Name="Computer Access Workflow"
     Description="My SharePoint Workflow"
     Id="6582541c-eb11-4904-9d35-1394b463f158"
     CodeBesideClass="POC_WorkFlow.CA_Workflow.CA_Workflow"
     CodeBesideAssembly="$assemblyname$">
    <Categories/>
    <MetaData>
      <AssociationCategories>List</AssociationCategories>
      <!-- Tags to specify InfoPath forms for the workflow; delete tags for forms that you do not have -->
      <!--<Association_FormURN>[URN FOR ASSOCIATION FORM]</Association_FormURN>
       <Instantiation_FormURN>[URN FOR INSTANTIATION FORM]</Instantiation_FormURN>
      <Task0_FormURN>[URN FOR TASK (type 0) FORM]</Task0_FormURN>
      <Task1_FormURN>[URN FOR TASK (type 1) FORM]</Task1_FormURN>-->
      <!-- Modification forms: create a unique guid for each modification form -->
      <!--<Modification_[UNIQUE GUID]_FormURN>[URN FOR MODIFICATION FORM]</Modification_[UNIQUE GUID]_FormURN>
      <Modification_[UNIQUE GUID]_Name>[NAME OF MODIFICATION TO BE DISPLAYED AS A LINK ON WORKFLOW STATUS PAGE</Modification_[UNIQUE GUID]_Name>
      -->
      <StatusPageUrl>_layouts/WrkStat.aspx</StatusPageUrl>
    </MetaData>
  </Workflow>

</Elements>

Now open up your Event Receiver in the feature that deploys your workflow and add the following code to the feature activated method

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    Guid workflowID = new Guid("6582541c-eb11-4904-9d35-1394b463f158");

    SPSite site =properties.Feature.Parent as SPSite;
   
    if (site != null)
    {
        SPWeb web = site.RootWeb;
        // OTB List for Tasks
        SPList taskList = web.Lists["Workflow Tasks"];

        // OTB Hidden List
        SPList historyList = web.Lists["Workflow History"];

        // List our workflow gets attached to
        SPList caList = web.Lists["List1"];

        SPWorkflowTemplate wfTemplate = web.WorkflowTemplates[workflowID];
        SPWorkflowAssociation wfAssociation =
            SPWorkflowAssociation.CreateListAssociation(wfTemplate, "Computer Access", taskList, historyList);

        wfAssociation.AllowManual = true;
        wfAssociation.AutoStartCreate = true;
        wfAssociation.AutoStartChange = true;

        caList.WorkflowAssociations.Add(wfAssociation);
        wfAssociation.Enabled = true;
    }
}