Just another WordPress.com site

Archive for the ‘Association’ Category

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.

     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);

SharePoint Workflow : Pass Association & Initiation parameters to SharePoint workflow programmatically

Hi All,

I worked on SharePoint workflow and explore many things.

Here I am going to share about SharePoint parameters i.e. ‘Association Data’ and ‘Initiation Data’.

Association Data: – Association data is declared when administrator creating an association on site/list. This data is helpful to provide some default value, some constraints values or some other information.

Initiation Data: – When a user starts a workflow, some information input is required from the user. These inputs may override the association data default values or some additional input. These inputs are specific to current workflow instance and not available to other instances.

When workflow starts using SharePoint UI, workflow’s forms are prompted (if any input is required). But sometimes we need to pass this information programmatically.  In this blog I am going to share my knowledge about “Pass parameters to workflow programmatically”.

Workflow persist the data in the form of xml, but to use xml in code is difficult, so i am using classes to manage data in the code and further I convert the class object into xml by serialization.

1) Create a public class with at least default parameter less constructor.

2) Mark the class as serializable at the top of the class by [Serializable] attribute. This attribute allow class to be serialize.

3) Add public properties, each correspond to required input parameters.

4) Add two static methods for serialization and deserialization.

4) By xml serialization convert the class into xml.

5) Assign the xml to workflow data.

A sample class MyWFInitiationData with two properties ‘Amount’ and ‘ApplicantEmail’

public class MyWFInitiationData {

public MyWFInitiationData(){


public string ApplicantEmail{

get; set;


public int Amount{

get;   set;


// As required can add more properties corresponding to the input from the user. MyWFInitiationData

static stringInitiationDataToXml(MyWFInitiationData param) {

string data = string.Empty;

MyWFInitiationData objParams = param;

MemoryStream stream = new MemoryStream();

XmlSerializer serial = newXmlSerializer(typeof(MyWFInitiationData));

serial.Serialize(stream, objParams);

Byte[] bytes = new Byte[stream.Length];

bytes = stream.GetBuffer();

data =Encoding.UTF8.GetString(bytes);

return data;


// Deserialize the initiation data from xml to MyWFInitiationData object.

static MyWFInitiationData InitiationDataFromXml(string initiationXML) {

if (initiationXML.Length > 0) {

XmlSerializer serializer = new XmlSerializer(typeof(MyWFInitiationData));

XmlTextReader textReader = new XmlTextReader(new System.IO.StringReader(initiationXML));

MyWFInitiationData reminderInitData = new MyWFInitiationData();

object obj = serializer.Deserialize(textReader);

if(obj != null)

reminderInitData = (MyWFInitiationData) obj;
return reminderInitData;



return null;




===============Start a workflow programmatically============

MyWFInitiationData data = new MyWFInitiationData();

string initiationDataXML = MyWFInitiationData.InitiationDataToXml(data);

SPWorkflowAssociation workflowAssosiation  = web.GetAssociationByName(“Your workflow name”, [System.Globalization.CultureInfo]::CurrentCulture);

SPWeb web =SPContext.Current.Web;

//if your workflow is associated with any listitem, you should pass the listitem reference instead of ‘web.Site.RootWeb’.

web.Site.WorkflowManager.StartWorkflow(web.Site.RootWeb, workflowAssosiation, initiationDataXML,

==========Get Data inside the workflow=================

You de-serialize the data from xml to MyWFInitiationData class object at workflow activated event as mention below

MyWFInitiationData _initData = MyWFInitiationData.InitiationDataFromXml(workflowProperties.InitiationData);

Now using _initData.Amount & _initData.ApplicantEmail we can get the values inside the workflow.