Workflow REST API: Retrieve available workflows

This sample shows how to retrieve a list of available workflows that the authenticated user has rights to start. Note the use of DataContract classes to deserialize the workflow.

Copy

Retrieve available workflows

//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;

//construct the endpoint for the workflows operation. Your URL will likely be different.
string operationEndPoint = @"https://k2.denallix.com/api/workflow/v1/workflows";

//retrieve the authenticated user's available workflows as JSON
string response = k2WebClient.GetStringAsync(operationEndPoint).Result;

//process the JSON response using a JSON deserializer.
//In this case the built-in .NET DataContractSerializer class (you may want to use JSON.NET instead)
//instantiate the DataContract used to parse the returned JSON worklist
Workflows workflows = new Workflows();

//Deserialize the response into the workflow object, using the Workflows_WorkflowsContract.Workflows data contract.
using (System.IO.MemoryStream memstream = new MemoryStream(Encoding.UTF8.GetBytes(response))) {
 System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new DataContractJsonSerializer(workflows.GetType());
memstream.Position = 0;
 workflows = (Workflows) serializer.ReadObject(memstream);
}
//do something with each item in the collection of workflows that the user has rights to
foreach (Workflow workflow in workflows.K2Workflows) {
 Console.WriteLine("Workflow ID: " + workflow.Id.ToString());
 Console.WriteLine("Workflow Name: " + workflow.Name);
 Console.WriteLine("Folder: " + workflow.Folder);
 Console.WriteLine("System Name: " + workflow.SystemName);
 if (workflow.Name == "Sample Workflow REST API") {
  sampleWorkflowDefinitionId = workflow.Id;
 }
 Console.WriteLine("**************");
}

//Data contract classes for the workflows operation
namespace WorkflowRestAPISamples.Workflows_WorkflowsContract
{
    //collection of workflows returned from the GET /workflows endpoint.
    [System.Runtime.Serialization.DataContract]
    public class Workflows
    {
        [DataMember(Name = "itemCount")]
        public long ItemCount
        { get; set; }

        [DataMember(Name = "workflows")]
        public List<Workflow> K2Workflows
        { get; set; }
    }

    //Workflow object returned from the GET /workflows endpoint.
    [System.Runtime.Serialization.DataContract]
    public partial class Workflow
    {
        [DataMember(Name = "id")]
        public int Id
        { get; set; }

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

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

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

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