How to: Create a Custom Activity for Workflow Manager 1.0
Updated: July 10, 2013
Activities are the core unit of behavior in WF and are the building blocks with which developers author workflows. In Workflow Manager 1.0, custom activities and workflows are composed out of types in the Trusted Surface, which is a set of trusted types and activities available for authoring activities and workflows. In this step in the Getting Started Tutorial, a custom activity that retrieves products from the Northwind sample database hosted at http://services.odata.org/Northwind/Northwind.svc
is created. This activity is then used in following steps in the tutorial.
Note
To watch a video walkthrough, or download the starter files and a completed version of the tutorial, see Workflow Manager 1.0 - Getting Started Tutorial.
In this Tutorial Step
Create the Visual Studio Solution and Activity Library Project
Create the GetProducts Activity
Create the Visual Studio Solution and Activity Library Project
Open Visual Studio 2012 and choose New, Project from the File menu.
Expand the Other Project Types node in the Installed, Templates list and select Visual Studio Solutions.
Select Blank Solution from the Visual Studio Solutions list. Ensure that .NET Framework 4.5 is selected in the .NET Framework version drop-down list. Type
WFMgrGettingStarted
in the Name box and then click OK.Right-click WFMgrGettingStarted in Solution Explorer and choose Add, New Project.
Tip
If the Solution Explorer window is not displayed, select Solution Explorer from the View menu.
In the Installed node, select Visual C#, Workflow. Ensure that .NET Framework 4.5 is selected in the .NET Framework version drop-down list. Select Activity Library from the Workflow list. Type
GetProductsActivities
in the Name box and then click OK.Note
Depending on which programming language is configured as the primary language in Visual Studio, the Visual C# node may be under the Other Languages node in the Installed node.
Right-click Activity1.xaml in Solution Explorer and choose Delete. Click OK to confirm.
Right-click GetProductsActivities in Solution Explorer and select Add Reference.
Click the Browse button and navigate to C:\Program Files\Reference Assemblies\Microsoft\Workflow Manager\1.0
Select Microsoft.Activities.dll, click Add, and then click OK.
In Solution Explorer, expand Properties under the GetProductsActivities project and double-click AssemblyInfo.cs to view the code.
Add the following using statement at the top of the file with the other using statements (Snippet1).
using System.Windows.Markup;
Tip
Snippet files are provided for the tutorial. To use them, download the starter files for the tutorial from Workflow Manager 1.0 - Getting Started Tutorial. In Visual Studio, choose Code Snippets Manager from the Tools menu. Select Visual C# from the Language drop-down, and then click the Add button. Browse to the location where you extracted the starter files and select the Assets\WFMgrGettingStarted folder. Click Select Folder, and then click OK. In your code file, right-click at the point at which you wish to insert the snippet, and select Insert Snippet. Scroll down the list and double-click WFMgrGettingStarted, and then double-click the desired snippet.
Add the following line of code at the bottom of the file (Snippet2).
[assembly: XmlnsDefinition("wf://workflow.windows.net/$Current/$Activities", "GetProductsActivities")]
Important This step is very important, and the second parameter must match the namespace of the project that contains the custom activities. If these steps are not performed, any workflows that use this custom activity will fail to publish with an exception similar to the following exception:
System.Xaml.XamlObjectWriterException: Cannot create unknown type '{clr-namespace:GetProductsActivities}GetProducts'.
Press Ctrl+Shift+B to build the solution.
Create the GetProducts Activity
In this section, the GetProducts
custom activity is created. The GetProducts
activity uses an HttpSend
activity to query the Northwind oData service hosted at http://services.odata.org/Northwind/Northwind.svc
, and convert the results from the service into a DynamicValue
. The GetProducts
activity then drills down into the returned DynamicValue
and uses the GetDynamicValueProperty<T>
activity to return the results of the keyword search.
Right-click GetProductsActivities in Solution Explorer and choose Add, New Item.
In the Installed, Visul C# Items node, select Workflow. Select Activity from the Workflow list.
Type
GetProducts
into the Name box and then click Add.Double-click GetProducts.xaml in Solution Explorer to display it in the designer if it is not already displayed.
Click Arguments in the lower-left side of the activity designer to display the Arguments pane.
Click Create Argument.
Type
SearchKeyword
into the Name box, select In from the Direction drop-down list, select String from the Argument type drop-down list, and then press ENTER to save the argument.Click Create Argument.
Type
ProductsData
into the Name box that is underneath the newly addedSearchKeyword
argument, select Out from the Direction drop-down list, and select Browse for types from the Argument type drop-down list.Type
DynamicValue
in the Type Name box, select DynamicValue from the results list, and click OK.Tip
If DynamicValue does not appear in the search results, ensure that you have added a reference to Microsoft.Activities.dll as described in Create the Visual Studio Solution and Activity Library Project.
Drag a Sequence activity from the Control Flow section of the Toolbox and drop it onto the Drop activity here label of the GetProducts activity designer.
Tip
If the Toolbox window is not displayed, select Toolbox from the View menu.
Drag an HttpSend activity from the Messaging section of the Toolbox and drop it onto the Drop activity here label of the Sequence activity.
Tip
Instead of browsing the Toolbox for activities you can also type the name of the desired activity into the Search Toolbox box at the top of the Toolbox.
Click the HttpSend activity in the workflow designer to select it, and configure the following properties in the Properties window.
Property Value Method GET Uri "http://services.odata.org/Northwind/Northwind.svc/Products?$format=application/json;odata=verbose&$filter=substringof('" + SearchKeyword + "', ProductName) eq true" ResponseContent ProductsData Tip
If the Properties window is not displayed, select Properties Window from the View menu.
Drag a GetDynamicValueProperty<T> activity from the DynamicValue section of the Toolbox and drop it on the Sequence activity so that it follows the HttpSend activity.
In the Select Types window that appears once the activity is dropped, select Microsoft.Activities.DynamicValue from the drop-down list. If DynamicValue is not in the list, select Browse for Types, type DynamicValue in the Type Name box, select DynamicValue from the results list, and click OK. Once DynamicValue is selected in the drop-down list click OK.
Click the GetDynamicValueProperty<T> activity in the workflow designer to select it, and configure the following properties in the Properties window.
Property Value PropertyName "d/results" Result ProductsData Source ProductsData Press CTRL+SHIFT+B to build the solution.
To see an example of the completed activity, and a video walkthrough of this tutorial step, see Workflow Manager 1.0 - Getting Started Tutorial
For instructions on how to create a workflow using this activity, see the next step in the tutorial, How to: Create a Workflow.