SetStateActivity and CorrelationTokens
Moving onto the setState1
activity, we can see that the CorrelationToken property is invalid.
CorrelationTokens are an important aspect of WF workflows and are used
to determine the scope of the activity. A CorrelationToken is simply a
text value that is used to group activities and, more importantly, the
properties that they use. If a workflow has a property named foo, for
example, when a workflow activity with a correlation token of token A
writes to the foo property, other workflow actions with a correlation
token of token A will be able to read the value. However, if a workflow
action with a correlation token of token B writes to the property, the
actions with a token of token A will still see the original value,
whereas actions with token B will see the new value. In effect, every
property is actually an array with the correlation token being used as
an indexer. When it comes to SharePoint workflows, this is particularly
relevant when using Task activities.
Set the CorrelationToken for setState1 to workflowToken with an OwnerActivityName of External_Calculation.
Adding Custom Status Values to SharePoint
Although the configuration of the setState activity
is now valid, it doesn’t quite do what we want it to. When a workflow
is added to a list item in SharePoint, a new column is added to the
appropriate list that shows the current state of the workflow. The
setState activity allows us to specify the value that should appear in
this list. In our case, we want to show the text Awaiting Calculation Result.
Before we can display a custom value for workflow
status, we need to let SharePoint know what our new value is. Workflow
states are stored in SharePoint in a similar format to lookup values,
so each state needs an ID and a text value. To add new states, we need
only the text value, since SharePoint will automatically generate a new
ID.
We can add the text for the new state in the
Elements.xml file that exists under our External Calculation workflow
in the Solution Explorer pane. Add an ExtendedStatusColumnValues
element to the MetaData element, as shown:
<MetaData>
<AssociationCategories>List</AssociationCategories>
<StatusPageUrl>_layouts/WrkStat.aspx</StatusPageUrl>
<ExtendedStatusColumnValues>
<StatusColumnValue>Awaiting Calculation Result</StatusColumnValue>
</ExtendedStatusColumnValues>
</MetaData>
With the text value for our new status added, we can
now configure our setState activity to use it. Unfortunately, it’s not
quite as simple as that though. SetState expects the ID of our new
state, and since this will be generated by SharePoint when our workflow
is installed, we don’t currently have a reference to the ID value. We
can calculate the ID for our new state simply by adding one to the ID
of the last state that SharePoint defines internally.
Right-click the workflow designer and then select View Code to see the code behind for our workflow. Add the following field:
public int AwaitingCalculationState = ((int) SPWorkflowStatus.Max);
SPWorkflowStatus
is an enumeration of the workflow states that SharePoint provides by
default. Our new extended status column value will be assigned the next
ID in the sequence, which we can retrieve using the SPWorkflowStatus.Max value. If we wanted to add more than one additional state, we could use SPWorkflowStatus.Max +1, SPWorkflowStatus.Max+2, and so on, to determine the IDs for subsequent states.
We can now bind the State property of our SetState activity to our new AwaitingCalculationState field.
HandleExternalActivity
The next activity to configure is
handleExternalEventActivity1. This activity listens for an event from a
pluggable workflow service. It’s configured in much the same way as
callExternalMethodActivity1.
Set the InterfaceType to IExternalCalculationService, and then select CalculationComplete from the EventName drop-down.
Since this is an event, we don’t need to specify any parameters. Although, since we’re interested in the CalculationResultArgs parameter that is passed with the event, we can bind this value to a local variable using the e
property. Click the ellipsis to show the Bind dialog. This time, select
the Bind To New Member tab and then add a new property with the name CalculationCompleteArgs, as shown. This binding will store the values passed with the event in a local variable called CalculationCompleteArgs that we’ll be able to use in subsequent workflow activities.
LogToHistoryListActivity
We’ve now configured all the mandatory properties
for our workflow. Before we deploy, we need to make one additional
change. The logToHistoryListActivity1 action is not set up to log
anything useful to the workflow history list. Since we want it to pick up the calculation result, we need to set a few properties.
For HistoryOutcome, type Calculation Result.
Using the Bind dialog, bind the HistoryDescription property to CalculationCompleteArgs.Result, as shown:
CodeActivity
The CodeActivity can be used to execute arbitrary
code within the workflow engine. Our workflow requires that, as a
result of the external calculation process, the Environmental
Compliance flag is set to true or false. For the purposes of our
demonstration, we’ll assume that our organization manufactures only
environmentally friendly products and that the result of the
calculation always indicates compliance.
To configure the CodeActivity for the ExecuteCode property, enter UpdateFlag.
Press enter, and the designer switches to code-behind view and a new method is automatically created for UpdateFlag. Add the following code:
private void UpdateFlag(object sender, EventArgs e)
{
workflowProperties.Item["Environmental Compliance"] = true;
workflowProperties.Item.Update();
}