Implementing error handling in the workflow activity

To implement error handling 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 error information for the custom workflow action.

For more information about error handling in workflow actions, see Supporting error handling.

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 error handling.

To add error handling to a workflow activity

  1. Add and register three 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.

    // Indicates whether error handling is enabled for this 
    // activity by the Workflow designer.
    public static DependencyProperty CaptureErrorsProperty = 
      DependencyProperty.Register("CaptureErrors", 
        typeof(bool), typeof(CustomActivity));
    
    // Indicates whether an error occurred. This value is provided to
    // the workflow adapter for the error occurrence workflow variable.
    public static DependencyProperty ErrorOccurredOutputProperty = 
      DependencyProperty.Register("ErrorOccurredOutput", 
        typeof(bool), typeof(CustomActivity));
    
    // Stores the details of an error if one occurred. This value is
    // provided to the workflow adapter for the error text workflow variable.
    public static DependencyProperty ErrorMessageOutputProperty = 
      DependencyProperty.Register("ErrorMessageOutput", 
        typeof(string), typeof(CustomActivity));
  2. Implement three corresponding public properties in the same class, using the getter and setter code shown in the following example.

    public bool CaptureErrors
    {
      set { base.SetValue(CaptureErrorsProperty, value); }
      get { return (bool)base.GetValue(CaptureErrorsProperty); }
    }
    
    public bool ErrorOccurredOutput
    {
      set { base.SetValue(ErrorOccurredOutputProperty, value); }
      get { return (bool)base.GetValue(ErrorOccurredOutputProperty); }
    }
    
    public string ErrorMessageOutput
    {
      set { base.SetValue(ErrorMessageOutputProperty, value); }
      get { return (string)base.GetValue(ErrorMessageOutputProperty);
    }
  3. Implement error handling in the Execute method of the same class, by using a try/catch block as shown in the following example. The CaptureErrors dependency property determines whether error handling is enabled. If the try block runs successfully, and error handling is enabled, then the ErrorOccurredOutput dependency property should be set to false. If an exception occurs in the try block, and error handling is enabled, then the catch block should set the ErrorOccurredOutput dependency property to true, and then set the ErrorMessageOutput dependency property to a string representation of the exception.

    Tip: Nintex recommends that the dependency property for the error text workflow variable is set to the Message property of the Exception class that represents the error, but this is not required. Any descriptive string can be supplied, as needed.

    protected override ActivityExecutionStatus Execute(
        ActivityExecutionContext executionContext)
    {
        // Confirm that the workflow activity is allowed to execute.
        ActivityActivationReference.IsAllowed(this, __Context.Web);
    
        // Get the resolved workflow context for the workflow activity.
        NWWorkflowContext ctx = NWWorkflowContext.GetContext(
            __Context, 
            new Guid(__ListId), 
            __ListItem.Id, 
            WorkflowInstanceId, 
            this);
    
        // Report the start of the workflow activity.
        base.LogProgressStart(ctx);
    
        // Perform the workflow activity.
        try
        {
            // TODO: Your code goes here.
    
            // The workflow activity has successfully executed.
            // Check if error handling is enabled.
            if (CaptureErrors)
            {
                // Set the dependency property for the error occurrence
                // workflow variable to false.
                ErrorOccurredOutput = false;
            }
    
            // Report the successful excution of the workflow activity.
            base.LogProgressEnd(ctx, executionContext);
    
            // Set the execution status of the workflow activity.
            return ActivityExecutionStatus.Closed;
        }
        catch (Exception ex)
        {
            // The workflow activity has encountered an exception.
    
            // Check if error handling is enabled.
            if (CaptureErrors)
            {
                // Set the dependency properties for the error occurrence
                // and error text workflow variables.
                ErrorOccurredOutput = true;
                ErrorMessageOutput = ex.Message;
    
                // Set the execution status of the workflow activity.
                return ActivityExecutionStatus.Closed;
            }
            else
            {
                // Throw the exception to the workflow manager, 
                // stopping execution of the workflow.
                throw;
            }
        }
    }

See Also

Concepts

Workflow activities

Workflow action adapters

Configuration pages

Operations

Working with workflow actions

Working with workflow activities