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.
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:
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
:
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.
History
- 19th May, 2009: Initial post