Workflow asmx Web Service Samples

This topic contains some sample code showing how to interact with the workflow .asmx Web Service.

These samples assume that you have added a project reference called
WorkflowRuntimeAsmxService
to your project, which points to the URL of the workflow .asmx web service, something similar to http://[K2ServerName]/K2Services/ws.asmx

Starting a new workflow with the .asmx web service

WorkflowRuntimeAsmxService.WSSoapClient client = new WSSoapClient();

//NOTE: you have to set up credentials for the service call. 
client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Delegation;

//instantiate a process instance
ProcessInstance K2ProcInst = new ProcessInstance();

//you set the workflow to start by setting the process name value
K2ProcInst.FullName = @"[ProjectName]\[WorkflowName]";

//set up the process instance"s values (Folio and Data fields)
K2ProcInst.Folio = "some folio value";

//get the defined datafields for the workflow
DataField[] K2Datafields = K2ProcInst.DataField;

//set up the data fields. In this case we will set each field"s value to the same value
foreach (DataField K2DataField in K2Datafields)
{
K2DataField.Value = "some field value";
}
//after setting up the workflow, start the new workflow instance
client.StartNewProcessInstance(ref K2ProcInst,false);
		

Retrieving the worklist with the .asmx web service

WorkflowRuntimeAsmxService.WSSoapClient client = new WSSoapClient();
client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Delegation;

//retrieve the worklist for the connected user
WorklistItem[] K2worklist = client.OpenWorklist(false);

//iterate over each worklist item
foreach (WorklistItem K2WLItem in K2worklist)
{
//do something with each worklist item
}
		

Opening and completing worklist item using the .asmx web service

WorkflowRuntimeAsmxService.WSSoapClient client = new WSSoapClient();
					
					//NOTE: you have to set up credentials for the service call. 
					client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Delegation;
					
					//open a worklist item with a specific serial number
WorklistItem K2WorklistItem = client.OpenWorklistItem("serialNumber", false);
					
					//update some properties of the worklist item/workflow
					K2WorklistItem.ProcessInstance.Folio = "updated Folio value";
					
					//get the defined datafields for the workflow
DataField[] K2Datafields = K2WorklistItem.ProcessInstance.DataField;
					
					//set up the data fields. IN this case we will set each field"s value to the same value
foreach (DataField K2DataField in K2Datafields)
					{
					K2DataField.Value = "some updated field value";
					}
					
					//complete the worklist item with the specified Action
					client.ExecuteActionByWorklistItem(K2WorklistItem,"selectedAction",false);
					
					//you can also complete the worklist item without setting data with the ExecuteActionBySerial method call
					client.ExecuteActionBySerial("serialNumber"