Authoring a basic approve-decline process with code
You can use the SourceCode.Workflow.Authoring, SourceCode.Workflow.Design, SourceCode.Workflow.VisualDesignersand SourceCode.Framework.Deployment assemblies to programmatically author and deploy workflows using code. To help describe these, we have provided a sample Visual Studio project with source code that demonstrates how to:
- Author a workflow
- Deploy a Workflow
- Perform management tasks on a deployed workflow
The following code sample on Github contains a sample Visual Studio project that demonstrates the authoring of a workflow with code: K2Documentation.Samples.Workflow.Authoring. The sample project is provided for demonstration purposes only. It is not supported by K2 product support and is not intended to be used as-is in production environments.
In this sample project, we will programmatically create a simple approval process using a series of helpers and factories contained within the WorkflowAuthoringFactory project. This project contains the DeploymentManager, K2FieldFactory, PropertyWizardNames, WizardNames, WorkflowFactory and WorkflowHelpers to make developing processes more modular.
The K2FieldFactory class allows you to create K2 fields to hold values in the process. In the example a field part is created to hold the serial number of the client event task item. Once the process is generated, opening it up in a K2 designer the field part is displayed in the wizard as follows:
K2 Fields and K2 Field Parts form what can be thought of as the communication layer between the K2 design time and runtime environments. An integral part of this layer is the Resolver Framework. The SourceCode.ResolverFramework along with the SourceCode.Workflow.Data.Resolvers assemblies form the foundation of this communication layer. Some of the items that can be resolved by the framework include:
- Simple values
- Data fields
- XML fields
- SmartObjects
- Environment variables
The PropertyWizardNames and WizardNames allow you to reference the default activities and events with constants instead of the display name or GUID
The WorkflowFactory and WorkflowHelpers allow you to write cleaner code by abstracting the code that registers wizard definitions for particular activities, events, rules, and populates wizard pages with values that you specify when designing the process programmatically.
The main form of this Windows application allows you to specify a process name and location, as well as to deploy the process and perform some process management tasks on the server.
After authoring the process, browse to the specified location of the project file and open the .kprx file in K2 Studio or K2 Designer for Visual Studio. The final process should look similar to the following process.