K2 REST Services: C# Samples

This content applies to legacy components (such as K2 Studio and K2 for Visual Studio), legacy assemblies, legacy services or legacy functionality. If you have upgraded from K2 blackpearl 4.7 to K2 Five, these items may still be available in your environment. These legacy items may not be available in new installations of K2 Five. These legacy items may also not be available, supported, or behave as described, in future updates or versions of K2. Please see the legacy component support policy for more information about support for these components.

You can write C# code to interact with the K2 Workflow REST service, as demonstrated by the following code snippets.

Starting a new workflow

//set the environment URLs
string BaseK2Uri = @ "https://k2.denallix.com/k2services/REST.svc";
string ProcessStartRestUrl = @ "Process/Instances/StartInstance";

//Build up a XML string to send to rest service
StringBuilder sb = new StringBuilder();
sb.Append(@ "<?xml version='1.0' encoding='utf-8'?><w:ProcessInstance xmlns:w='http://schemas.k2.com/worklist/d1' xmlns:p='http://schemas.k2.com/process/d1'");
sb.Append(@ " FullName='[folder\workflowname]'");
sb.Append(" Folio='[foliovalue]'");
sb.Append(" Priority='1'>");

//add datafields, if needed
//string field sample
sb.Append("<p:DataField Name='[StringDataFieldName]'>[StringDataFieldValue]</p:DataField>");
//integer field sample
sb.Append("<p:DataField Name='[IntegerDataFieldName]'>[IntegerDataFieldValue]</p:DataField>");

sb.Append("</w:ProcessInstance>");

// post XML to process service
System.Threading.Tasks.Task < HttpResponseMessage > response;
System.Net.Http.HttpClientHandler handler = new HttpClientHandler();
handler.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; {
 using(HttpClient client = new HttpClient(handler)) {
  string resourceAddress = ProcessStartRestUrl;
  //if you want to start sync, append ?synchronous=true to the URL;
  client.BaseAddress = new Uri(BaseK2Uri + "/");

  //start an async call to the service. Normally you would use more advanced threading for this approach
  response = client.PostAsync(resourceAddress, new StringContent(sb.ToString(), Encoding.UTF8, "text/xml"));
  response.Wait();

  if (!response.Result.IsSuccessStatusCode) {
   throw new Exception(response.Result.StatusCode.ToString());
  }
 }
}

Retrieving the worklist

//set the environment URLs and namespaces
string BaseK2Uri = @ "https://k2.denallix.com/k2services/REST.svc";
string WorklistRestUrl = @ "/Worklist/Items?piDataField=true&actXmlField=true";
System.Xml.Linq.XNamespace WorklistNamespace = @ "http://schemas.k2.com/worklist/d1";
System.Xml.Linq.XNamespace ActivityNamespace = @ "http://schemas.k2.com/activity/d1";
System.Xml.Linq.XNamespace ProcessNamespace = @ "http://schemas.k2.com/process/d1";
System.Xml.Linq.XNamespace EventNamespace = @ "http://schemas.k2.com/event/d1";

//create the web request
System.Net.HttpWebRequest request = WebRequest.Create(BaseK2Uri + WorklistRestUrl) as HttpWebRequest;
//Assign Credentials
request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

//get response as a XML document
System.Xml.Linq.XDocument responseDocument;
using(System.Net.HttpWebResponse response = request.GetResponse() as HttpWebResponse)
using(System.IO.Stream responseStream = response.GetResponseStream())
using(System.Xml.XmlReader responseReader = XmlReader.Create(responseStream)) {
 responseDocument = XDocument.Load(responseReader);
}

//build up a LINQ query to extract data from the XML response
var worklistItemsQuery =
 from worklistItemCollection in responseDocument.Elements(WorklistNamespace + "WorklistItemCollection")
from worklistItem in worklistItemCollection.Elements(WorklistNamespace + "WorklistItem")
from processInstance in worklistItem.Elements(ProcessNamespace + "ProcessInstance")
select new {
 serialNumber = (string) worklistItem.Attribute("SerialNumber"),
  folio = (string) processInstance.Attribute("Folio"),
};

//do something with the returned items
foreach(var worklistItem in worklistItemsQuery) {
 Console.WriteLine("Folio:" + worklistItem.folio + " | SerialNo:" + worklistItem.serialNumber);
}