Posted by: Cirilo Meggiolaro | 03/28/2009

Tip of the day #165 – Custom WWF activities

A custom activity is a container that you may use to either drag and drop existing activities and set specific behaviors or define methods and properties that dictates the activity behavior. You may use the custom activity several times in your workflow.

Imagine a component that logs information to a database when something goes wrong in your application. You have a single component that you call from several places in your code. The custom activity has the same effect in a workflow.

Today we are going to create a simple custom activity that writes a message to a console. It is really simple but gives you all the steps you need to follow to put the custom activity up and running.

How to…

  • Open Microsoft Visual Studio;
  • Let’s start creating a library project that will store all your custom activities. From the File menu select New > Project;
  • Select Workflow Activity Library project. Select a name and a location to save the project and click OK;
  • An activity named Activity1 is already added to the project. Rename the activity to ConsoleWriteLineActivity;
  • Opening it, you will see the base container for your custom activity;
Picture 1 - Custom activity designer

Picture 1 - Custom activity designer

  • Press F7 or right-click and select View Code. Create a dependency property named Message that will store the message to be displayed. You can type propd and press Tab key twice to use the code snippet for the dependency property or type the code completely (!?!). The code must be similar to the following:

public string Message
    get { return (string)GetValue(MessageProperty); }
    set { SetValue(MessageProperty, value); }

public static readonly DependencyProperty MessageProperty =
DependencyProperty.Register(“Message”, typeof(string), typeof(ConsoleWriteLineActivity));

  • From the toolbox drag a code activity onto the activity surface and rename it to WriteMessageActivity;
  • Now you need to hook up an event to handle the execution of the code activity. On the Properties window beside the ExecuteCode item type a name to the event handler and press Enter;
  • Inside the event handler add a Console.WriteLine statement and pass the Message property as parameter. The final event handler must be similar to the following:

private void WriteMessageActivity_ExecuteCode(object sender, EventArgs e)

  • Compile the project;
  • After just a few steps your activity is ready to be used in workflows. You are able to view the custom activity in the toolbox now;

Consuming the activity

  • Let’s create one simple workflow to consume the activity;
  • Add a Sequential Workflow Console Application project to the solution;
  • Add a reference to the ActivityLibrary;
  • Open the default Workflow1;
  • One of the ways to send the string the custom activity we created is waiting for is to store the text in a property in the workflow class and bind it to the dependency property available in the custom activity. Press F7 or right-click and select ViewCode. Add a regular string accessor that will be responsible for storing the message that will be displayed:

public string TextToBeDisplayed { get; set; }

  • Drag a Code activity onto the workflow surface;
  • Double click the code activity or type a name on the Properties window for the ExecuteCode item and press Enter to create the event handler for the ExecuteCode event. Define a text and store it to the TextToBeDisplayed property. Your code will be similar to the following:

private void codeActivity1_ExecuteCode(object sender, EventArgs e)
    TextToBeDisplayed = “Have you seen the new XNA Game Studio?”;

  • From the Toolbox drag a ConsoleWriteLineActivity onto the workflow designer surface;
Picture 2 - The custom activity available in the toolbox

Picture 2 - The custom activity available in the toolbox

Picture 3 - The custom activity added to the workflow

Picture 3 - The custom activity added to the workflow

  • You may have noticed the lock icon on the ConsoleWriteLineActivity. You have access only to the custom activity properties. You cannot edit it from inside the workflow;
  • The last step is to hook up the Message property by clicking on the browse button (the one with the three dots) beside the Message property on the Properties window. The Bind dialog will be displayed;
Picture 4 - The Bind dialog

Picture 4 - The Bind dialog

  • Select the TextToBeDisplayed property and click OK;
  • Compile and run your application. The text “Have you seen the new XNA Game Studio?” is displayed on the Console window.

Congratulations! You have created a custom activity that you may use in your workflows. It’s just the start. You may create more dependency properties as you need or create a custom object to pass to the workflow.

Tomorrow we are going to take a look on how to change the look and feel of a custom activity to be prettier on both toolbox and designer.

Stay tuned!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: