Click here to Skip to main content
13,258,763 members (49,253 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 19 May 2009

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

, 19 May 2009
Rate this:
Please Sign up or sign in to vote.
How to access the workflow variables within a custom activity in a Windows Workflow Foundation (WF) workflow.


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)
               ActivityBind workflowActivityBind = new ActivityBind();
               workflowActivityBind.Name = activity.Name;
               workflowActivityBind.Path = valueName;
               value = workflowActivityBind.GetRuntimeValue(activity);
          { }
          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 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)
               ActivityBind workflowActivityBind = new ActivityBind();
               workflowActivityBind.Name = activity.Name;
               workflowActivityBind.Path = valueName;
               workflowActivityBind.SetRuntimeValue(activity, value);
          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.


  • 19th May, 2009: Initial post


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


About the Author

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

You may also be interested in...


Comments and Discussions

Questioncommunication between workflow and application Pin
gargabhay1015-May-17 0:42
membergargabhay1015-May-17 0:42 
GeneralWill do the job Pin
Diego Resnik11-Nov-10 2:35
memberDiego Resnik11-Nov-10 2:35 
GeneralThanks - Some usefully helpers Pin
Member 36226513-Sep-10 1:59
memberMember 36226513-Sep-10 1:59 
GeneralThanks for sharing Pin
Sriharsha Vardhan20-Jun-09 3:49
memberSriharsha Vardhan20-Jun-09 3:49 
Generalgood Pin
Savara20-May-09 7:27
memberSavara20-May-09 7: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 19 May 2009
Article Copyright 2009 by Jeff W Welch
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid