Windows Workflow 4: Adding Items to a Collection

I wanted to create a workflow that executes several custom activities in parallel and adds the results from each to a shared collection. This is easily accomplished by using WF4’s AddToCollection activity.

The XAML below demonstrates the use of both the Parallel and AddToCollection activities to accomplish this task. I’m also using of the ForEach activity to iterate through my collection to display the results.

<Activity xmlns="" 
      <Variable x:TypeArguments="scg:List(x:String)" Default="[New List(Of String)]" Name="MyList" />

        <AddToCollection x:TypeArguments="x:String" Collection="[MyList]" Item="item1" />
        <AddToCollection x:TypeArguments="x:String" Collection="[MyList]" Item="item2" />
        <AddToCollection x:TypeArguments="x:String" Collection="[MyList]" Item="item3" />
    <ForEach x:TypeArguments="x:String" Values="[MyList]">
        <ActivityAction x:TypeArguments="x:String">
                <DelegateInArgument x:TypeArguments="x:String" Name="item" />
            <WriteLine Text="[item]" />

Here’s the output produced by running this workflow:

Some notes about this example:

  • The XAML was written without the use of Visual Studio’s designer
  • There is a collection variable of type List<string> declared named “MyList”; this requires us to import the namespace System.Collections.Generic
  • The AddToCollection activities’ Collection properties are bound to MyList
  • The ForEach activity’s Values property is bound bound to MyList
  • The ForEach activity creates a delegate argument named “item” which is then passed in to the ForEach activity’s WriteLine activity
%d bloggers like this: