Workflow REST API: Starting a workflow

This sample shows how to start a new workflow instance. Note the use of DataContract classes to serialize the workflow and its datafields.

Copy

Starting a workflow

//use a System.Net.Http.HttpClient to send the request.
//TODO: You will have to construct the HttpClient and authentication for the webclient before running the snippet
System.Net.Http.HttpClient k2WebClient;

//TODO: set the workflow definition ID of the workflow you want to start.
string workflowID = "22";

//build the URI and URL for the workflow instance endpoint. Your value will likely be different.
string operationEndPoint = @"https://k2.denallix.com/api/workflow/v1/workflows/" + workflowID

//build up the workflow instance object (we will serialize this object later and pass it to the endpoint)
//see the class definition below for example of the WorkflowInstance class. in this sample we created a class definition for the specific workflow to start
WorkflowInstance wfInstance = new WorkflowInstance();

//TODO: set variables and other values for the new workflow instance
wfInstance.Folio = System.DateTime.Now.Ticks.ToString();
wfInstance.Priority = 1;
//TODO: instantiate and populate the datafields in the workflow, using the DataFields class for this specific workflow definition
//Note the data conversions.
//Your values and datafields will likely be different to these
wfInstance.DataFields = new DataFields();
wfInstance.DataFields.BooleanVariable = true;
wfInstance.DataFields.DateTimeVariable = System.DateTime.Now;
wfInstance.DataFields.DecimalVariable = (long) 101.99;
wfInstance.DataFields.NumberVariable = 99;
wfInstance.DataFields.TextVariable = System.DateTime.Now.Ticks.ToString();
wfInstance.DataFields.SampleSmartObjectRecordId = 1;
//set the ID of the reference item SmartObject.
//See https://help.k2.com/onlinehelp/k2five/DevRef/current/default.htm#Runtime/WF-REST-API/Workflow-REST-API-Item-References.htm for examples of setting item reference values
//in this example we're setting the record ID property of an item reference to record ID 1
//your values will likely be different to this example
wfInstance.itemReferences.Sample_Workflow_REST_API_SmartObject._1.ID = 1;

//serialize the workflow instance to JSON format and read it in
DataContractJsonSerializer ser = new DataContractJsonSerializer(wfInstance.GetType());
System.IO.MemoryStream ms = new System.IO.MemoryStream();
ser.WriteObject(ms, wfInstance);

string encodedJsonObject = Encoding.UTF8.GetString(ms.ToArray());
//NOTE: if your datafield names have spaces, you may need to edit the serialized string to add the spaces back in.
//e.g. encodedJsonObject = encodedJsonObject.Replace("TextDatafield", "Text Datafield");
System.Net.Http.StringContent datacontent = new System.Net.Http.StringContent(encodedJsonObject, Encoding.UTF8, "application/json");

//post the JSON data to the endpoint (for simplicity, we're not doing anything with threading and async here)
var result = k2WebClient.PostAsync(operationEndPoint, datacontent).Result;
//if you need to read in the resulting workflow instance ID, read the result content
var workflowInstanceId = await result.Content.ReadAsStringAsync().ConfigureAwait(false);

/*class definition for the workflow instance to start*/
//these properties are common for all workflow definitions so you should not need to change them
[DataContract]
class WorkflowInstance
{
    [DataMember(Name = "expectedDuration")]
    public long ExpectedDuration { get; set; }

    [DataMember(Name = "folio")]
    public string Folio { get; set; }

    [DataMember(Name = "priority")]
    public long Priority { get; set; }

    [DataMember(Name = "xmlFields")]
    public XmlField[] XmlFields { get; set; }

    [DataMember(Name = "dataFields")]
    public DataFields DataFields { get; set; }

    [DataMember(Name = "itemReferences")]
    public Itemreferences itemReferences { get; set; }
}

//class definition for workflow XML datafields
public partial class XmlField
{
    [DataMember(Name = "name")]
    public string Name { get; set; }

    [DataMember(Name = "value")]
    public string Value { get; set; }
}


/// <summary>
/// Class that describes the datafields (variables) defined in the workflow. This class is specific to the definition of the workflow
/// Review the variables in the workflow to set up the necessary properties for this class
/// note to set the .NET datatype for the DataMembers to the appropriate value that will parse to the underlying datafield/variable type
/// You can use the GET /api/workflow/v1/workflows/{id}/schema endpoint to discover the data fields defined for the workflow
/// </summary>
[DataContract]
public partial class DataFields
{
    //TODO: Define as many fields as needed based on the workflow definition.
    //[DATAFIELDNAME] is the name of a datafield/variable defined within your workflow.
    //[DataMember(Name = "[DATAFIELDNAME]")]
    //public string [DATAFIELDNAME] {get; set;}

    [DataMember(Name = "TextVariable")]
    public string TextVariable { get; set; }

    [DataMember(Name = "NumberVariable")]
    public int NumberVariable { get; set; }

    [DataMember(Name = "DateTimeVariable")]
    public DateTime DateTimeVariable { get; set; }

    [DataMember(Name = "BooleanVariable")]
    public Boolean BooleanVariable { get; set; }

    [DataMember(Name = "DecimalVariable")]
    public long DecimalVariable { get; set; }
}

/// <summary>
/// Class that describes the Item References defined in the workflow. This class is specific to the definition of the workflow
/// Review the item references in the workflow to set up the necessary properties for this class
/// You can use the GET /api/workflow/v1/workflows/{id}/schema endpoint to discover the item references defined for the workflow
/// </summary>
[DataContract]
public partial class Itemreferences
{
    //this is an example where the SmartObject System Name is Sample_Workflow_REST_API_Smartobject
    //and we only want to insert one item reference with an ID value that will allow the workflow
    //to retrieve the item reference's values at runtime
    public Sample_Workflow_REST_API_Smartobject Sample_Workflow_REST_API_SmartObject { get; set; }
}

/// <summary>
/// represents the first item in the collection
/// </summary>
public class Sample_Workflow_REST_API_Smartobject
{
    public _1 _1 { get; set; }
}

/// <summary>
/// represents the record ID of the first item in the collection
/// we could set this value when starting the workflow
/// </summary>
public class _1
{
    public int ID { get; set; }
}