With our pluggable workflow service and
external calculation engine up and running, we can create a basic
workflow in Visual Studio that will send messages to the calculation
engine. Our test workflow will be very simple; when a new item is added
to a list, its title will be sent to the
calculation engine. We will then be able to trigger a response manually
from the calculation engine, which will be passed to the workflow. The
workflow will log the response and complete.
We
can add our workflow to the WorkflowDemonstration solution that we
created earlier. In Visual Studio, select Project | Add New Item. From
the Add New Item dialog, select SharePoint | 2010 | Sequential
Workflow. Name the new workflow External Calculation, as shown:
In
the SharePoint Customization Wizard dialog, leave the default name as
WorkflowDemonstration - External Calculation. We can see that the same
types of workflow that are available in SharePoint Designer are also
available for Visual Studio workflows: List Workflow and Site Workflow.
Since we’re planning to use data in a list to trigger our workflow, set
the type to List Workflow.
Accept
the default association settings. This will associate our workflow with
the Products list that we created earlier. Click Finish to complete the
process.
1. Using the Visual Studio Workflow Designer
When the workflow has been added to the project, the
workflow designer tool will be displayed. You can see that its user
interface is similar to the one we created earlier using Visio 2010.
You can drag workflow activities from the toolbox on to the design
surface to build up the workflow logic.
Our workflow needs five additional steps: CallExternalMethod, which can be found in the Windows Workflow v3.0 group in the toolbox; SetState, which can be found in the SharePoint Workflow group; HandleExternalEvent, which can be found in the Windows Workflow v3.0 group; LogToHistoryListActivity, which can be found in the SharePoint Workflow group; and CodeActivity, which can be found in the Windows Workflow v3.0 group.
Drag the required activities onto the designer surface, as illustrated:
Configuring Workflow Activities
You can see from the designer that a few of our
activities have not been configured properly. This is indicated by the
icon in the upper-right corner of the activity control. Let’s work
through them in sequence to set the appropriate configuration details.
CallExternalMethodActivity
Starting with callExternalMethodActivity1, when we
select the activity we can see in the Properties pane that the values
for InterfaceType and MethodName are invalid as shown. This activity is
used to communicate with a pluggable workflow service and in our case
will be used to invoke the SubmitCalculation method on our
CalculationWorkflowService.
The first property to configure is InterfaceType. This is the interface that we tagged earlier with the ExternalDataExchange
attribute. Click the ellipsis to show the Browse and Select a .NET Type
dialog. The IExternalCalculationService is already selected since it’s
the only interface in our solution with the appropriate attribute.
Click OK to use this.
Now
the MethodName property needs to be configured. From the drop-down
list, select SubmitCalculation. The values in the drop-down list are
populated from the InterfaceType by using reflection. Since
SubmitCalculation is the only method on our interface, it is the only
item in the list.
Once
SubmitCalculation has been selected, a new property appears: product.
The property is added automatically since it appears in the list of
arguments for the SubmitCalculation method. For out test workflow,
we’ll set this to the title of the list items on which the workflow has
been started. Click the ellipsis to show the Bind dialog, an important
part of the workflow designer because it allows us to bind properties
to local variables or other properties. We can add new variables by
selecting the Bind To A New Member tab if required. For our purposes,
we need to bind the product property to the Title property of the
current workflow item. Expand workflowProperties | Item | Title, and
then click OK to store the binding.