Implementing multiple output in the workflow activity

To implement multiple output in the custom workflow activity for a custom workflow action, you must register, implement, and use dependency properties in the workflow activity that can interact with the workflow adapter to provide storage for the custom workflow action.

For more information about multiple output in workflow actions, see Supporting multiple output.

Note: The following examples refer to a custom workflow activity named CustomActivity, and a custom workflow adapter named CustomAdapter, for the purposes of describing how to implement multiple output.

To add multiple output to a workflow activity

  1. Add and register two new public DependencyProperty static variables to the workflow activity. The variables need to be added to the class derived from the ProgressTrackingActivity base class, as shown in the following example.

    // Implement a ValueStorageCollectionForWorkflow object as a dependency property, which stores the list of values to retrieve for output.
    public static DependencyProperty MultiOutputInfoProperty = 
        DependencyProperty.Register("MultiOutputInfo", typeof(ValueStorageCollectionForWorkflow), typeof(CustomActivity));
    
    // Implement a Hashtable as a dependency property, which stores the results after processing within the workflow activity.
    public static DependencyProperty MultiOutputProperty = 
        DependencyProperty.Register("MultiOutput", typeof(Hashtable), typeof(CustomActivity));;
  2. Implement two corresponding public properties in the same class, using the getter and setter code shown in the following example.

    public ValueStorageCollectionForWorkflow MultiOutputInfo
    {
        get { return (ValueStorageCollectionForWorkflow)base.GetValue(MultiOutputInfoProperty); }
        set { base.SetValue(MultiOutputInfoProperty, value); }
    }
    
    public Hashtable MultiOutput
    {
        get { return (Hashtable)base.GetValue(MultiOutputProperty); }
        set { base.SetValue(MultiOutputProperty, value); }
    }
  3. Implement storage handling in the Execute method of the same class, by using the highlighted code as shown in the following example. The highlighted code demonstrates how to read the MultiOutputValueInfo object to determine which values to store, and how to store the results in the correct format. Each ValueStorageInfo object contains a ValueIdentifier to represent the name of the data item to retrieve. It also contains a VariableName value which represents the name of the workflow variable that will contain the final value.

    As the action queries the value for each record in the ValueStorageItems array, Each record in the output Hashtable should use the variable name as the key, and the actual value of the item represented by ValueIdentifier.

    protected override ActivityExecutionStatus Execute(
      ActivityExecutionContext executionContext)
    {
      // Check if activity is allowed to run.
      ActivityActivationReference.IsAllowed(this, __Context.Web);
    
      // Resolve workflow references, if needed.
      // NWWorkflowContext ctx = NWWorkflowContext.GetContext(
           __Context, 
           new Guid(__ListId), 
           __ListItem.Id, 
           WorkflowInstanceId, 
           this);
    
      // Log the start of the activity execution.
      base.LogProgressStart(ctx);
      
      // Perform an action that retrieves information - database or list query, for example.
    
      // Store the output
      MultiOutput = new Hashtable();
      foreach (ValueStorage valueStorage in MultiOutputInfo.ValueStorageItems)
      {
        // The ValueIdentifier identifies the value the user intended to store. 
        // Workflow variables can be used to specify the value identifier therefore it must be run through AddContextDataToString.
        // The actual value is then added to the MultiOutput collection, with the variable name as the key.
    
        string valueIdentifierResolved = context.AddContextDataToString(valueStorage.ValueIdentifier);
        object result = GetReturnValueForKey(valueIdentifierResolved);
          ...
        MultiOutput.Add(valueStorage.VariableName, result);  
      }
      
      // Log the end of the activity execution.
      base.LogProgressEnd(ctx, executionContext);
    
     // Set the execution status of the activity.
     return ActivityExecutionStatus.Closed;
    
    }

See Also

Concepts

Workflow activities

Workflow action adapters

Configuration pages

Operations

Working with workflow actions

Working with workflow activities