Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#

Getting and Setting Workflow Variables from a Custom Activity, Windows Workflow Foundation

Rate me:
Please Sign up or sign in to vote.
3.00/5 (2 votes)
19 May 2009CPOL2 min read 45.1K   9   5
How to access the workflow variables within a custom activity in a Windows Workflow Foundation (WF) workflow.

Introduction

When building workflows, you inevitably get into a situation where the standard activities just don't cut it. In that case, you'll create a custom activity to do whatever intricate business logic you need. In the process of creating an activity of this nature, you'll add some properties, Dependency Properties, that allow the workflow to pass data to the activity. So what if you want to go in the opposite direction? How do you read or change the value of a workflow variable? Well these two methods will get you there.

Using the Code

The first method, GetValueOfWorkflowVariable, takes an activity and a path and returns the value as an object.

C#
public static object GetValueOfWorkflowVariable(Activity activity, string valueName)
{
     object value = null;
     if (activity != null)
     {
          try
          {
               ActivityBind workflowActivityBind = new ActivityBind();
               workflowActivityBind.Name = activity.Name;
               workflowActivityBind.Path = valueName;
               value = workflowActivityBind.GetRuntimeValue(activity);
          }
          catch
          { }
          if (value == null)
               value = GetValueOfWorkflowVariable(activity.Parent, valueName);
     }
     return value;
}

In order to get the value of a workflow variable, you have to create an ActivityBind object. With that, you can pass the name of an activity and then the Path to (or name of) the variable you want to access. Since a workflow is just a type of activity (a SequentialWorkflowActivity to be exact), this will work.

However, getting the value of a variable from an activity isn't the hard part. The hard part is finding what activity is the actual workflow. Since we don't have easy access to the workflow itself from inside an activity, we can only reference the activity's parent, and the parent could be a Sequence Activity, while Activity, or any other kind of composite activity. So in this code, if we can't find the variable in question, we search the parent. We keep traversing up the tree until we find a matching variable. If we get all the way to the top (or the activity is null), and we still haven't found it, we simply return null.

Here's an example of how you use it:

C#
String activityValue = "";
activityValue = GetValueOfWorkflowVariable(this.Parent, "ActivityValue").ToString();

In this example, I pass this.Parent as the activity because I know this activity isn't the workflow itself...so maybe its parent is.

Setting the value of a workflow variable is very much the same with a little less code. Here's that method, SetValueOfWorkflowVariable:

C#
public static void SetValueOfWorkflowVariable
	(Activity activity, string valueName, object value)
{
     if (activity != null)
     {
          try
          {
               ActivityBind workflowActivityBind = new ActivityBind();
               workflowActivityBind.Name = activity.Name;
               workflowActivityBind.Path = valueName;
               workflowActivityBind.SetRuntimeValue(activity, value);
          }
          catch
          {}
          SetValueOfWorkflowVariable(activity.Parent, valueName, value);
     }
}

Again we create an ActivityBind object and traverse the tree until we successfully set the value of the variable. If for some reason we can't find it, we exit the function with no error.

Here's an example of how to use it:

C#
String activityValue = "Some Value";
SetValueOfWorkflowVariable(this.Parent, activityValue);

And that's it, now whenever you need access to some workflow variable either to read or change, you need only include these methods and call them.

History

  • 19th May, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
ASP.NET developer specializing in development of rich web applications. My tools of choice: C#, JavaScript, CSS.

Comments and Discussions

 
Questioncommunication between workflow and application Pin
gargabhay1014-May-17 23:42
gargabhay1014-May-17 23:42 
GeneralWill do the job Pin
Diego Resnik11-Nov-10 1:35
Diego Resnik11-Nov-10 1:35 
GeneralThanks - Some usefully helpers Pin
Member 36226513-Sep-10 0:59
Member 36226513-Sep-10 0:59 
GeneralThanks for sharing Pin
Sriharsha Vardhan20-Jun-09 2:49
Sriharsha Vardhan20-Jun-09 2:49 
Thanks for sharing
Generalgood Pin
nørdic20-May-09 6:27
nørdic20-May-09 6:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.