So I have been building some custom SharePoint Designer activities, and testing them on a Virtual Machine of mine. I log into that machine as the System Administrator, and of course browse SharePoint. I noticed that for even the most simplest workflow, while I could start it manually, it wouldn’t start automatically no matter what. I searched through the logs and found this:
“Declarative workflows cannot automatically start if the triggering action was performed by System Account. Canceling workflow auto-start.”
Well, there ya go. If you are logged in as the System Administrator, workflows won’t automatically kick off. Simple fix.
A classic K2 Workflow goes like this:
An HR Department wants to automate their Leave Request Process. It is currently a paper based, manual endeavor which has led to some frustrating situations. They have been told that hosting this process in K2 will alleviate many of their issues.
In addition to simply building the workflow in K2, the HR Department wants entire submission stored in a SharePoint Form Library for future use. So, how do we do that?
Firstly, lets assume that this is what the workflow looks like:

Now let’s say that the requirement is to store the request in some “Approved” library if HR approves and “Rejected” if they reject. Here’s how to do that.
1 Create your form libraries. I named mine “Approved Leave Requests” and “Rejected Leave Requests” respectively.
2 Create two new views in the form. The first view shows the data of an approved form, the other for the rejected.
Approved:

Rejected:

3 Use the SharePoint Documents event inside the workflow to create a new document in each of the libraries.
Step 1: On event name/action step, select to Upload from a K2 field.

Step 2: Drag the root node of the InfoPath XML into the input field marked as “K2 Field”

Step 3: Choose the appropriate site, library, and content type (if necessary) for this upload. For the document name, I’m using the process folio. You can use whatever you want as long as you don’t forget to type “.xml” after it.

4 Set the view of the InfoPath document, before you upload it. Use the Data event to accomplish this.
Step 1: Select Transfer Data

Step 2: Set the “DocumentView” field to “Leave Request Approved” and “Leave Request Rejected” respectively.

And that’s it. You’ll see that when the document is created, and viewed, the Approved or Rejected view will be shown accordingly.
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.