May182009

Getting and Setting Workflow Variables From A Custom Activity, Windows Workflow Foundation

When building workflows you inevitably get into a situation where the built in activities just don’t cut it.  In that case, you’ll build a custom activity to do whatever intricate business logic that 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.  What happens, however, when you want to go in the opposite direction?  How do you read or change the value of a workflow variable?  Well these two functions will get you there.

GetValueOfWorkflowVariable:

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 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 activity.  Since we don’t have easy access to the workflow itself from inside an activity, we can only reference it’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:

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.

SetValueOfWorkflowVariable:

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:

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.

Page 1 of 1