Just another WordPress.com site

Posts tagged ‘Workflow Association’

How to programmatically start sharepoint workflow

SharePoint workflow is great way to automate the business process.  SharePoint workflow can be configured to start automatic or manually. But sometimes our requirement lies in between two default options i.e. we want to start workflow automatically if and only if certain condition met e.g.  workflow initiation depends on certain business logic or depends on some field value entered by user.  If condition met we need to start workflow otherwise not. In this scenario, SharePoint default options ( Automatic OR Manual) to start workflow do not work and developer requires to start workflow at run time using code.

To start a workflow at run-time  we need two important values: –

  1. Workflow Association Reference Id OR Workflow association name: – Workflow association id that we want to start. OR workflow association name can also use to identify it.
  2. Workflow Initiation Data: – Its optional but most of the workflow requires some initiation data. Initiation data that requires by workflow to take decision. To know more about the initiation data and how to pass it, please refer http://wp.me/p1nAw1-m

To start any workflow, we requires following objects: –

  1. WorkflowManager: – With each SPSite object a SPWorkflowManager object is associated , enabling you to centrally control the workflow templates and instances across the site collection.  We can get WorkflowManager instace by SPSite instace.
    E.g.

     WorkflowManager = SPContext.Current.Web.Site.WorkflowManager;
    
  2.  SPWorkflowAssociation: – Workflow association is a binding between list/library and workflow template.  When a workflow is added to SharePoint list/library an workflow association is crated. We can get the workflow association by list/library where workflow is associated.
  3. E.g.
      SPWorkflowAssociationCollection associationColl = ParentList.WorkflowAssociations;
      foreach (SPWorkflowAssociation association in associationCollection) {
        // We can find the association by BaseId or by workflow association name.
        // if (String.Compare(objWorkflowAssociation.BaseId.ToString("B"), {"Workflow_GUID"}, true) == 0)
       // you can identify the workflow association by association name, entered during association creation.
      if (association.Name == wfName)        {
        association.AutoStartChange = true;
        association.AutoStartCreate = false;
        // We can change the
        association.AssociationData = string.Empty;
        // We can use other overload StartWorkflow methods.
       spSite.WorkflowManager.StartWorkflow(listItem, association,  association.AssociationData);
      }
    }
    

So complete code to start workflow at runtime is

public void StartWorkflow(SPListItem listItem, SPSite spSite, string wfName)  {
    SPList parentList = listItem.ParentList;
    SPWorkflowAssociationCollection associationCollection = parentList.WorkflowAssociations;
    foreach (SPWorkflowAssociation association in associationCollection)  {
      if (association.Name == wfName){
       association.AutoStartChange = true;
       association.AutoStartCreate = false;
       association.AssociationData = string.Empty;
       spSite.WorkflowManager.StartWorkflow(listItem, association,  association.AssociationData);
       }
     }
}
Advertisements
Aside

SharePoint Workflow Association : Associate workflow to list or web programmatically

Hi,

It’s very frequently required to attach/associate workflow to list or web programmatically. For a workflow a history list and task list is required.

History list is use to log activity/error occurred during workflow running span.

Task list is use to create and assign task for some approval etc. to user.

Workflow Association:- The binding of any workflow template to list or web is called workflow association.

Step. 1 Create a workflow history list.

public void CreateHistoryList(){

     using(SPSite site = new SPSite(“Your web url”)){

        using(SPWeb web = site.OpenWeb()){

           web.Lists.Add(“YourHistoryListTitle”,”ListDescription”,SPListTemplateType.WorkflowHistory);

        }

     }

}

Step. 2 Create a Workflow Task list.

public void CreateTaskList(){

using(SPSite site = new SPSite(“Your web url”)){

        using(SPWeb web = site.OpenWeb()){

           web.Lists.Add(“YourTaskListTitle”,”ListDescription”,SPListTemplateType.Tasks);

        }

     }

}

Step. 3 Associate workflow with web ( In case of Site Workflow)

 public void AddWorkflowAssociation(){childWeb.AllowUnsafeUpdates = true;

using(SPSite site = new SPSite(“Your web url”)){

using(SPWeb web = site.OpenWeb()){


//Add workflow association
SPWorkflowTemplate workflowTemplate = web.WorkflowTemplates.GetTemplateByName(“YourWorkflowTemplateName”, new CultureInfo(1033));
SPWorkflowAssociation association = SPWorkflowAssociation.CreateListAssociation(workflowTemplate, “Workflow Association Name”,
web.Lists[“TaskListTitle”], web.Lists[“HistoryList”]);

//configures workflow assocition options
association.AllowManual = true;
association.AutoStartCreate = false;
association.AutoStartChange = false;
association.AllowAsyncManualStart = false;
web.WorkflowAssociations.Add(association);
web.Update();

}

     }

}

Step. 4 Associate workflow with web ( In case of List Workflow)

public void AddWorkflowAssociation(){childWeb.AllowUnsafeUpdates = true;

using(SPSite site = new SPSite(“Your web url”)){

        using(SPWeb web = site.OpenWeb()){

        //Add workflow association
        SPWorkflowTemplate workflowTemplate = web.WorkflowTemplates.GetTemplateByName(“YourWorkflowTemplateName”, new CultureInfo(1033));
        SPWorkflowAssociation association = SPWorkflowAssociation.CreateListAssociation(workflowTemplate, “Workflow Association Name”,
                                web.Lists[“TaskListTitle”], web.Lists[“HistoryList”]);

        //configures workflow assocition options
        association.AllowManual = true;
        association.AutoStartCreate = false;
        association.AutoStartChange = false;
        association.AllowAsyncManualStart = false;
        SPList list = web.Lists.TryGetList(“YourListTitle”);
        list.WorkflowAssociations.Add(association);
        web.Update();

         }

     }

}

Now your workflow is associated with either list or web.