How to Pass initiation data for sharepoint approval workflow to start programmatically


  •  SPSite siteCollection1 = new SPSite(SPSite);
                        using (SPWeb webSite1 = siteCollection1.AllWebs[WebSiteName])
                            Guid wfBaseId = new Guid("{078f3469-5a80-41ad-a19d-df097f70d0fa}");
                            //SPSite site = SPSite;
                            SPWeb web = siteCollection1.OpenWeb();
                            SPList list = web.Lists["Travel"];
                            SPListItem item = list.GetItemById(properties.ListItemId);
                            SPWorkflowAssociation associationTemplate = list.WorkflowAssociations.GetAssociationByBaseID(wfBaseId);

                            associationTemplate.AssociationData = "";
                            siteCollection1.WorkflowManager.StartWorkflow(item, associationTemplate, "<root />");

    I am using the above code to start an approval sharepoint workflow programmatically and now need initiation data like a list of approvers, cc, request field etc to be passed programmatically so that I can acutally start the workflow as it currently gives an error that I cannot pass null value. I know that this has to be done using xml but will appreciate if someone can show me how. I searched the net but the stuff I found does not work.

    8. srpna 2011 0:15


Všechny reakce

  • Hi Graham,

    Did you tried following code ?


    SPWorkflowAssociation wrkFl = docLib.WorkflowAssociations[new Guid("Guid of your workflow")];
    site.WorkflowManager.StartWorkflow(docLib.Item, wrkFl, wrkFl.AssociationData, true);


    Please note the fact that once you started the workflow you cannot modify the Associated data, most probably you are getting Assiciated data as null because you have not specified <AssociationData> block in your Workflow element.xml file so if you don't have any data you should still specify empty data to avoid null exception.






    Ashraf ul Islam
    Sharepoint Consultant
    8. srpna 2011 3:31
  • Hi Ashraf,

    It is actually the Association Data that I want to pre fill before I start the workflow. I want to know how this can be done using code.

    8. srpna 2011 4:00
  • Instead of passing null value you can pass the empty string to AssociationData and/or InitiationData.
    8. srpna 2011 5:35
  • @Graham: Use following in the code where you want to start workflow theorugh code:

    SPWorkflowManager WFM = spSite.WorkflowManager; 
    SPList spList = spWeb.Lists[listGuid];
    SPWorkflowAssociation WFA = spList.WorkflowAssociations.GetAssociationByBaseID(workflowId); 
    SPListItem spListItemForWF = spWeb.GetListItem(listItemURL);
    string data = "Your Initiation Data";
    SPWorkflow spWF = WFM.StartWorkflow(spListItemForWF, WFA, data, true);

    Inside Workflow, get initiation data as:hope it helps.


    string initiationData = this.workflowProperties.InitiationData;

    8. srpna 2011 6:29
  • Hi Farhan

    My question is how to fill in the data as you have mentioned:

    string data = "Your Initiation Data";

    So what do I fill in in the place of Your Initiation Data"
    8. srpna 2011 8:52
  • @Graham: Yes. You can pass your desired data as string. Make it comma or colon separated and get it inside workflow. Like:



    string data = "Approvers:CC:RequiredField";

    Inside workflow:

    string initiationData = this.workflowProperties.InitiationData;

    string[] data = initiationData.split(':'')

    9. srpna 2011 1:59

    Answer 1) This answer was once given to me by Ani Michal but I couldnt try it... Pls see this:-

    Lets you need two input from the user i.e. Amout & Date. 

    1) Create a serializable class lets WFInitialtion Data with Two public property Amount and Date.


    2) So place two user control on your custom aspx page, two user input control.


    3) As user clicks on form's submit click, get the value entered in input control and assign entered values in an intance of WFInitiationData's properties.


    4) Now convert the WFInitiationData's object into xml by serialization.


    5) Pass initiation data in the form of xml to workflow.


    6) Inside the workflow we get this initiation data in the form of xml from "workflowProperties.InitiationData". We need to change it back to WFInitiationData's instance by de-serialization.

     Do give me your feedback as I am also stuck on the same prob. of passing initiation data to a Workflow when we start it programatically onclick of button.


    Answer 2) If you donot want initiation data then you can follow the foll approach as given to me by Ashraf:-

    you are getting this error because AssociationData is Empty or null. You cannot set associated data on runtime as its readonly during execution, so you should follow the steps given below to supply dummy empty data for your workflow to starts with.

    Step 01:

    Open Element.xml of your workflow.

    Just after <Category/> tag enter following xml




    Step 02:

    Remove the workflow associated with list.

    Re-deploy your workflow.

    The WF will run but without any initiation data.


    9. srpna 2011 5:30
  • Hi,


    I managed to fix the problem as follows: (Hope this helps someone someday... :)

    string InitiationData = "<dfs:myFields xmlns:xsd=\"\" xmlns:dms=\"\" xmlns:dfs=\"\" xmlns:q=\"\" xmlns:d=\"\" xmlns:ma=\"\" xmlns:pc=\"\" xmlns:xsi=\"\"><dfs:queryFields></dfs:queryFields><dfs:dataFields><d:SharePointListItem_RW><d:Approvers><d:Assignment><d:Assignee><pc:Person><pc:DisplayName>";

    InitiationData = InitiationData + FirstApproverName + "</pc:DisplayName><pc:AccountId>" + FirstApproverLogin;

    InitiationData = InitiationData + "</pc:AccountId><pc:AccountType>User</pc:AccountType></pc:Person></d:Assignee><d:Stage xsi:nil=\"true\" /><d:AssignmentType>Serial</d:AssignmentType></d:Assignment><d:Assignment><d:Assignee><pc:Person><pc:DisplayName>";

    InitiationData = InitiationData + SecondApproverName + "</pc:DisplayName><pc:AccountId>" + SecondApproverLogin + "</pc:AccountId><pc:AccountType>User</pc:AccountType></pc:Person></d:Assignee><d:Stage xsi:nil=\"true\" xmlns:xsi=\"\" /><d:AssignmentType>Serial</d:AssignmentType></d:Assignment></d:Approvers><d:ExpandGroups>true</d:ExpandGroups><d:NotificationMessage>";

    InitiationData = InitiationData + WorkFlowMessage + "</d:NotificationMessage><d:DueDateforAllTasks xsi:nil=\"true\" /><d:DurationforSerialTasks>1</d:DurationforSerialTasks><d:DurationUnits>Day</d:DurationUnits><d:CC><pc:Person><pc:DisplayName>";

    InitiationData = InitiationData + ccName + "</pc:DisplayName><pc:AccountId>" + ccLogin + "</pc:AccountId><pc:AccountType>User</pc:AccountType></pc:Person></d:CC><d:CancelonRejection>true</d:CancelonRejection><d:CancelonChange>false</d:CancelonChange><d:EnableContentApproval>false</d:EnableContentApproval></d:SharePointListItem_RW></dfs:dataFields></dfs:myFields>";

    • Označen jako odpověď Graham1135 9. srpna 2011 22:31
    9. srpna 2011 22:31
  • Graham, please can u explain how u solve the issue?

    i want to pass only 1 value from initiation form to my workflow...

    as the workflow is started programatically on button click, i cannot get the inititation form...

    12. srpna 2011 5:15
  • pls help me... i have triedYou cannot vote on your own post:-


    but i get error on this line:-

    return Encoding.UTF8.GetString(bytes);  i get error as the name 'Encoding" doesnot exist in the current context.!

    13. srpna 2011 10:42

  • UTF8Encoding utf8 = new UTF8Encoding();

    Byte[] encodedBytes = utf8.GetBytes(message);

    message = encodedBytes.ToString();

    return (message);

    here s da solution:-





    15. srpna 2011 5:11
  • Note that you can generate the classes required by opening the Workflow Initiation InfoPath form from SharePoint Designer, export the data as source files, and use the XSD tool as described here: (written for InfoPath 2007 and VS2005 but I used the same technique with InfoPath 2010 and VS2010).  The only difference between what I did and what is described on that page is that I included all files with an extension of .xsd instead of just the myschema.xsd and added a namespace. i.e.

    xsd builtincontrolsschema.xsd choices.xsd context.xsd dfschema.xsd qfschema.xsd rootschema.xsd typeschema.xsd /c /l:cs /n:<mynamespace>

    Also, the values didn't actually pass into the workflow with the entire <dfs:myFields>...</dfs:myFields> structure - I directly serialized the <d:SharePointListItem_RW>...</d:SharePointListItem_RW> item. Here's the code I used:

                    var data = new <namespace>.SharePointListItem_RW()
                        <set properties here>

                    string eventData = getInitXmlString(data);
                    web.Site.WorkflowManager.StartWorkflow(listItem, workflow, eventData, true);


            private string getInitXmlString(<namespace>.SharePointListItem_RW data)
                return getInitXmlString(new System.Xml.Serialization.XmlSerializer(typeof(<namespace>.SharePointListItem_RW)), data);

            private string getInitXmlString(System.Xml.Serialization.XmlSerializer ser, object data)
                using (var ms = new System.IO.MemoryStream())
                    ser.Serialize(ms, data);
                    ms.Position = 0;
                    byte[] bytes = new byte[ms.Length];
                    ms.Read(bytes, 0, bytes.Length);
                    return Encoding.UTF8.GetString(bytes);

    13. června 2012 22:17
  • Hi Graham,

    I found this working for me, but having a little issue.

    Now, workflow is getting automatically started successfully on Item Added with Signers and Cc from list columns, but when a Signer clicks on open this task in the email and open the document, the document is having missing Open this Task button in Document Information Panel (DIP).

    Do you know how to fix that issue?

    Thanks in advance.



    Thanks, RP

    22. srpna 2017 14:20