Programmatically define Activity color and layout

This content applies to legacy components (such as K2 Studio and K2 for Visual Studio), legacy assemblies, legacy services or legacy functionality. If you have upgraded from K2 blackpearl 4.7 to K2 Five, these items may still be available in your environment. These legacy items may not be available in new installations of K2 Five. These legacy items may also not be available, supported, or behave as described, in future updates or versions of K2. Please see the legacy component support policy for more information about support for these components.

When using the workflow authoring API to programmatically define a workflow, you may want to set an activity's colors or location of the activity on the canvas. The following code sample demonstrates how you can work with the layout of activities programmatically.

This code snippet requires references the assemblies:

  • SourceCode.Workflow.Authoring
  • SourceCode.Workflow.Design
  • System.Drawing
//load the process into memory
Process k2Process = SourceCode.Workflow.Authoring.Process.Load("[filename");

//load the activity you want to work with. In this example we grab the first activity
Activity act = k2Process.Activities[0];

//instantiate a ProcessViewActivityLayoutData object that defines the layout of an activity on the canvas
ProcessViewActivityLayoutData actLayout = new ProcessViewActivityLayoutData();
//load up an activity object by Guid if you want to work with an existing activity
actLayout.Name = "{" + act.Guid.ToString() + "}";

//if you want to set the location of the activity, define it in x, y coordinates
int x = 100;
int y = 200;
actLayout.Location = new System.Drawing.Point(x, y);

//apply a colour to the activity
actLayout.MainColor = System.Drawing.Color.DarkBlue;

//add layout data to the process
((ProcessView) k2Process.Views[0]).ActivitiesLayoutData.Add(actLayout);