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.

public static void Get_Workflows() {
 //shows how to retrieve the list of workflows that the authenticated user has rights to
 //you should be familiar with web request authentication and JSON to use this API

 //define the URI and URL for the workflows endpoint
 //URL for the K2 server
 string K2ServerURL = "https://k2.denallix.com";
 string workflowsEndpointURI = @ "/Api/Workflow/V1/workflows";
 string workflowsURL = K2ServerURL + workflowsEndpointURI;

 //set up client and credentials
 //we use static windows credentials here for brevity, see authentication samples for other auth mechanisms
 string userID = "administrator@denallix.com";
 string pwd = "K2pass!";
 System.Net.WebClient webclient = new System.Net.WebClient();
 webclient.Credentials = new System.Net.NetworkCredential(userID, pwd);

 //retrieve the authenticated user's available workflows as JSON
 string response = webclient.DownloadString(workflowsURL);

 //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 tasklist object, using the K2TaskList data contract.
 //see below this code snippet for example of the 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 the collection of tasks in the retrieved task list
 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);
  Console.WriteLine("**************");
 }
}


// workflows defined as it comes back as a call to the workflows REST 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;  }
}

// Workflows object defined as it comes back for the workflows call.
[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;  }
}

}