How to start a workflow programmatically in the InfoPath code using Workflow web service

How to start an Approval workflow programmatically in the InfoPath form code?

  1. Create a web reference that points to https://yourSiteUrl/_vti_bin/workflow.asmx .
  2. Configure an Approval workflow in your form library and capture the template id.

From form library settings->workflow settings, click the workflow name, right click in the page to View Source. Search for “templateid”. The TemplateID looks like the following:

TemplateID=%7b5C65AB69-11BC-430D-B730-71050250F576%7d

(“%7b” is encoded for “{“ and “%7d” is encoded for “}”)

GUID for the template id is : 5C65AB69-11BC-430D-B730-71050250F576

  1. Write the InfoPath code. The highlighted parts in the sample code below needs to be replaced. 

**TIP: If you can't get the workflowParamers XML string right, you can write a simple console app in server using OM. SPWorkflowAssociation.AssociationData will return the init form data that you already configured in the list/library workflow settings.

 

public void submit_OnClick(DocActionEvent e)

        {

            // Write your code here.

            WorkflowService.Workflow workflowService = new WorkflowService.Workflow();

            workflowService.Url = "https://siteURL/_vti_bin/workflow.asmx";

            workflowService.Credentials = System.Net.CredentialCache.DefaultCredentials;

            workflowService.PreAuthenticate = true;

            try

      {

                XmlDocument workflowParameters = new XmlDocument();

                string strXml = "<my:myFields xml:lang='en-us' xmlns:xsi='https://www.w3.org/2001/XMLSchema-instance' xmlns:my='https://schemas.microsoft.com/office/infopath/2003/myXSD'><my:Reviewers><my:Person><my:DisplayName>Approver’s display name</my:DisplayName><my:AccountId>domain\\account</my:AccountId><my:AccountType>User</my:AccountType></my:Person></my:Reviewers><my:CC></my:CC><my:DueDate xsi:nil='true'></my:DueDate><my:Description></my:Description><my:Title></my:Title><my:DefaultTaskType>1</my:DefaultTaskType><my:CreateTasksInSerial xsi:nil='true'></my:CreateTasksInSerial><my:AllowDelegation>true</my:AllowDelegation><my:AllowChangeRequests>true</my:AllowChangeRequests><my:StopOnAnyReject xsi:nil='true'></my:StopOnAnyReject><my:WantedTasks xsi:nil='true'></my:WantedTasks><my:SetMetadataOnSuccess xsi:nil='true'></my:SetMetadataOnSuccess><my:MetadataSuccessField></my:MetadataSuccessField><my:MetadataSuccessValue></my:MetadataSuccessValue><my:ApproveWhenComplete xsi:nil='false'></my:ApproveWhenComplete><my:TimePerTaskVal xsi:nil='true'></my:TimePerTaskVal><my:TimePerTaskType xsi:nil='true'></my:TimePerTaskType><my:Voting>false</my:Voting><my:MetadataTriggerField></my:MetadataTriggerField><my:MetadataTriggerValue></my:MetadataTriggerValue><my:InitLock xsi:nil='false'></my:InitLock><my:MetadataStop xsi:nil='true'></my:MetadataStop><my:ItemChangeStop xsi:nil='true'></my:ItemChangeStop><my:GroupTasks>false</my:GroupTasks></my:myFields>";

                workflowParameters.LoadXml(strXml);

//Guid for the workflow templateId

Guid guid = new Guid("{5C65AB69-11BC-430D-B730-71050250F576}");

//form URL needs to be replaced

XmlNode result = workflowService.StartWorkflow("https://siteURL/formlibrary/test.xml", guid, workflowParameters);

                if (result != null)

                    thisXDocument.UI.Alert("Result: " + result.Value);

               

            }

            catch (SoapException ex)

            {

       thisXDocument.UI.Alert("SoapException: " + ex.Message + " " + ex.Detail.Value);

            }

            catch (Exception ex0)

            {

                thisXDocument.UI.Alert("Exception: " + ex0.Message);

            }

           

        }