This article was created in response to a support issue logged with K2. The content may include typographical errors and may be revised at any time without notice This article is not considered official documentation for K2 software and is provided “as is” with no warranties.

Objective

The following article explains how to use the REST service to start a process instance:

https://help.k2.com/onlinehelp/k2blackpearl/DevRef/4.7/default.htm#WorkflowRESTServices.htm

However, in some scenarios you will need to populate a datafield before starting the instance.

Before You Begin

Before you begin you will need the following:

  • A process that you would like to start
  • A Datafield to populate
  • The K2 Services URL.

How-to Steps

Please find the Powershell Script below:

$k2server = "K2_BASE_ADDRESS";     #e.g. https://k2.domain.com
$k2project = "K2_PROJECT";
$k2workflow = "K2_PROCESS";


function executeRequest() {
$apiUrl = getApiUrl
$body = getContentXml
$result = Invoke-RestMethod -UseDefaultCredentials -Method POST -Uri $apiUrl -Body $body -ContentType 'application/xml'
}

function getApiUrl() {
return "$($k2server)/k2services/REST.svc/Process/Instances/StartInstance";
}

function getContentXml() {

$DFValue = "DATAFIELD_VALUE";

return "<?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'" +
" FullName='$($k2project)\$($k2workflow)'>" +
"<p:DataField Name='MyProcessDataField'>$($filename)</p:DataField>" +
"</w:ProcessInstance>";


}

executeRequest

All values highligted in YELLOW need to be updated to reflect the values on your K2 Server.