Workflow WCF Services Samples
This topic contains some sample code showing how to interact with the workflow WCF Web Service.
You can download a sample project that demonstrates using the Workflow WCF service from here: K2Documentation.Samples.WorkflowRuntime.WorkflowServicesWCF.zip.The sample project is provided for demonstration purposes only. It is not supported by K2 product support and is not intended to be used as-is in production environments.
These samples assume that you have added a project reference called
WorkflowRuntimeWCFService
to your project, which points to the URL of the workflow WCF web service, something similar to http://[k2server]/K2Services/WCF.svc
WorkflowRuntimeWCFService.ProcessNavigationServiceClient processSvcClient = new ProcessNavigationServiceClient();
//instantiate a process instance to hold the ProcessInstance object
ProcessInstance k2ProcInst = new ProcessInstance();
//provide the path to the workflow that you want to start
k2ProcInst.FullName = @"[ProjectFolder]\[Process Name]"; //e.g. k2ProcInst.FullName = @"K2Samples\Sample Workflow";
//set the folio (optional)
k2ProcInst.Folio = "[Folio Value]";
//set the datafields if needed, by declaring a List of DataField
List <DataField> dataFieldsCollection = new List <DataField>();
//populate the data field values by adding items to the DataField List
//string field example
dataFieldsCollection.Add(new DataField() { Name = "[StringFieldName]", Value = "[FieldValue]" });
//date field example. Dates require special massaging, otherwise you get XMLAny errors.
//In this sample we’ll take the current date.
//Note the use of the XmlConvert function. You may need to do this with other datafield types as well.
dataFieldsCollection.Add(new DataField() { Name = "[DateFieldName]", Value = XmlConvert.ToString(DateTime.Now, XmlDateTimeSerializationMode.Local) });
//Once datafields values are set, pass the collection to datafields into the process instance object’s DataField array
k2ProcInst.DataField = dataFieldsCollection.ToArray();
//start the workflow
//Note: if you defined process datafields or XML fields, set the appropriate boolean value to true
processSvcClient.StartNewProcessInstance(k2ProcInst, false, false, false, true, false);
//Note: some versions of K2 may require you to handle an unexpected "to open" error, e.g.
try
{
processSvcClient.StartNewProcessInstance(k2ProcInst, false, false, false, true, false);
}
catch (Exception ex)
{
if (!ex.Message.Contains("to open"))
{ throw; }
}
//get the worklist items for current user
WorkflowRuntimeWCFService.WorklistItem[] K2WorklistItems = svcWorklist.OpenWorklist(false, false, false, false);
//iterate over the collection of worklist items
foreach (WorkflowRuntimeWCFService.WorklistItem K2WorklistItem in K2WorklistItems)
{
//Do something with the worklist item
string serialNumber = K2WorklistItem.SerialNumber;
}
WorkflowRuntimeWCFService.WorklistNavigationServiceClient svcWorklist = new WorkflowRuntimeWCFService.WorklistNavigationServiceClient();
//the WCF Service's Criteria object works similarly to the .NET API's WorklistCriteria object
WorkflowRuntimeWCFService.Criteria criteria = new WorkflowRuntimeWCFService.Criteria() {
//create a filter criteria to limit items returned
Filter = new [] {
new WorkflowRuntimeWCFService.CriteriaFilter() {
Field = WorkflowRuntimeWCFService.CriteriaField.ProcessFolio,
Comparison = WorkflowRuntimeWCFService.CriteriaComparison.Equal,
Value = "Folio to Search For",
ValueType = WorkflowRuntimeWCFService.ValueType.String
},
new WorkflowRuntimeWCFService.CriteriaFilter() {
Logical = WorkflowRuntimeWCFService.CriteriaLogical.And,
Field = WorkflowRuntimeWCFService.CriteriaField.ProcessData,
SubField = "Data Field Name",
Comparison = WorkflowRuntimeWCFService.CriteriaComparison.Like,
Value = "*Data Field Value*",
ValueType = WorkflowRuntimeWCFService.ValueType.String
}
},
//create a sort criteria to sort items returned
Sort = new [] {
new WorkflowRuntimeWCFService.CriteriaSort() {
Field = WorkflowRuntimeWCFService.CriteriaField.ProcessStartDate,
Order = WorkflowRuntimeWCFService.CriteriaSortOrder.Ascending
}
}
};
//return the worklist using the criteria
WorkflowRuntimeWCFService.WorklistItem[] K2WorklistItems = svcWorklist.OpenWorklistFiltered(criteria, false, false, false, false);
//iterate over the collection of worklist items
foreach(WorklistItem K2WorklistItem in K2WorklistItems) {
Console.WriteLine(K2WorklistItem.ProcessInstance.Folio);
}
//and perform request/response-style processing
System.ServiceModel.EndpointAddress URL = new EndpointAddress("http://k2.denallix.com/K2Services/WCF.svc/Worklist");
WorkflowRuntimeWCFService.IWorklistNavigationService SvcWorklist1 = new WorkflowRuntimeWCFService.WorklistNavigationServiceClient("BasicHttpBinding_IWorklistNavigationService", URL) as WorkflowRuntimeWCFService.IWorklistNavigationService;
WorkflowRuntimeWCFService.OpenWorklistRequest Request = new WorkflowRuntimeWCFService.OpenWorklistRequest();
WorkflowRuntimeWCFService.OpenWorklistResponse Response = new WorkflowRuntimeWCFService.OpenWorklistResponse();
Response = SvcWorklist1.OpenWorklist(Request);
WorklistItem[] K2WorklistItems1 = Response.OpenWorklistResult;
//iterate over the collection of worklist items
foreach (WorklistItem K2WorklistItem in K2WorklistItems1) {
Console.WriteLine(K2WorklistItem.ProcessInstance.Folio);
}
//open the worklist item
WorklistItem K2WorklistItem = SvcWorklist.OpenWorklistItem("serialNumber", true, true, true, true, true);
//get some data from the worklist item
string Folio = K2WorklistItem.ProcessInstance.Folio;
//iterate over the collection of datafields for the process instance
foreach(DataField dataField in K2WorklistItem.ProcessInstance.DataField) {
//do something with the datafield
string fieldvalue = dataField.Value;
}
//get the possible actions for the worklist item
WorkflowRuntimeWCFService.Action[] K2Actions = K2WorklistItem.Action;
//setting the value for a specific datafield before completing the worklist item
DataField someDataField = (DataField) K2WorklistItem.ProcessInstance.DataField.Where(x => x.Name == "DataFieldName").First();
//set the field value. Remember to use XML convert for fields like dates, rich text and so on
someDataField.Value = "new datafield value";
//complete the worklist items
SvcWorklist.ExecuteActionByWorklistItem(K2WorklistItem, "Selected Action", false);
//you could also complete the task with a serial number and action only
//without instantiating a worklist item object, like so:
SvcWorklist.ExecuteActionBySerial("serialNumber", "Selected Action", false);
//open the process instance
WorkflowRuntimeWCFService.ProcessInstance pi = svcProcNav.OpenProcessInstance("1", true, true); //change the "1" to your process instance
//get the view flow URL
Console.WriteLine(pi.ViewFlow);