Try our conversational search powered by Generative AI!

Loading...
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Recommended reading 

This topic describes the customization of activity flows. To replace or customize an activity flow:

  1. Create and build a new activity flow in a Visual Studio project.
  2. Add existing or new activities to the flow.

Classes in this topic are available in the following namespaces:

Configuration

Configure the activity flows in a .CS file (the config file \Configs\ecf.workflow.config is no longer needed).

An activity flow should inherit from the ActivityFlow class, and you can decorate it with an ActivityFlowConfiguration attribute.

[ActivityFlowConfiguration(Name = “Example”)]
public class ExampleActivityFlow : ActivityFlow

Each activity flow should have a unique name. If the Name parameter is not provided, the engine gets the ActivityFlow class name. In this example, Name is ExampleActivityFlow. The activity flow name will be used to look up and execute.

Example: ExecutionManager.ExecuteActivityFlow(workflowName, parameters)

An activity flow should implement a Configure method to declare which activities are executed, and how they are executed. You can configure an activity flow to override another flow of the same name and mode by setting a higher priority. The default priority is zero (0). For example, you configure two flows with the same name and all in beta mode:

[ActivityFlowConfiguration(Name = “Example”)]
public class BetaExampleActivityFlow : ActivityFlow
[ActivityFlowConfiguration(Name = “Example”, Priority = 1)]
public class AlphaExampleActivityFlow : ActivityFlow

When the activity flow “Example” is called in beta mode, it creates an instance of AlphaExampleActivityFlow class, not BetaExampleActivityFlow. This means that only the AlphaExampleActivityFlow is registered for the “Example” activity flow.

Note: You can register a workflow without the ActivityFlowConfiguration attribute. If a workflow is inherited from the ActivityFlow class and not decorated by the attribute, it is still registered with same name as the name of class.

Creating a new activity to send notifications

The following example creates a new workflow activity, then uses it in a new workflow. It uses this workflow to replace the shipped CartCheckout workflow. The new activity sends an automated email to store customers after an order completion. This example illustrates how to update and customize a flow.

Creating a new activity

  1. Create a Workflow Activity Library project in Visual Studio. In this example, we named the project CustomActivityLibrary1.
  2. Add a new activity to the activities project for the email. Name the Activity SendNotification.
  3. Set the inheritence for the activity to Mediachase.Commerce.WorkflowCompatibility.Activity.
  4. Make sure you installed the EPiServer.Commerce.Core NuGet package.
  5. Add the code into the new activity's code-behind file (SendNotification.cs) from the Code Sample section in this document. Ensure the naming is correct, that you create the stub of the method to send the email, and that your method is called in the activity Execute method. A stub for the method is provided in the SendEmailNotification() method. See the SendEmails method code in the /Templates/Everything/BusinessControls/CheckoutControls/CheckoutWizardModule.cs file.

    Note: Use the activity OrderGroup object reference to retrieve the information about the order to be put in the email. To generate the formatted copy of the email body text with the order information, use the Mediachase.Commerce.Engine.Template.TemplateService. See sending Order Notifications for information about setting up and using the template service.

  6. Build the project.

Adding the activity to the activity flow

Add the activity created above (along with other activities contained in the Mediachase.Commerce.Workflow.dll) to a new activity flow. Use this activity flow to replace the CartCheckoutActivityFlow (Mediachase.Commerce.Workflow.CartCheckoutActivityFlow).

  1. Create a Activity Flow project in Visual Studio. In this example, name the project MyCustomCartCheckout.
  2. Make sure you installed EPiServer.Commerce.Core NuGet package.
  3. Add a class to the customCartCheckout project. In this example, name it CustomCartCheckout. Set the inheritence for the activity flow to Mediachase.Commerce.Engine.ActivityFlow. And decorate the class with ActivityFlowConfiguration attribute. By setting the Name as CartCheckout, you override the default implementation of Mediachase.Commerce.Workflow.CartCheckoutActivityFlow.
  4. In the activity flow file (in this example named CustomCartCheckout.cs), implement the Configure method to configure the list of activities to be executed, and add the last step to use the SendNotification activity. See the relevant code in the code sample section in this document.
  5. Build the project.
  6. Locate the newly-built assembly and copy it to the project's bin folder.
  7. Recompile, and access the website where this activity is used during checkout.

Examples

Example: starting code for the SendNotification activity.

C#
using Mediachase.Commerce.Engine;
using Mediachase.Commerce.Orders;
using Mediachase.Commerce.WorkflowCompatibility;
using System;

namespace CustomActivity
{
    public partial class SendNotification : Activity
    {
        ///
        /// Gets or sets the order group.
        ///
        ///The order group.
        [ActivityFlowContextProperty]
        public OrderGroup OrderGroup { get; set; }

        ///
        /// Called by the workflow runtime to execute an activity.
        ///
        ///The  to associate with this  and execution.
        /// 
        /// The  of the run task, which determines whether the activity remains in the executing state, or transitions to the closed state.
        /// 
        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            try
            {
                // Validate the properties at runtime
                this.ValidateRuntime();

                //Send notification
                this.SendEmailNotification();

                // Retun the closed status indicating that this activity is complete.
                return ActivityExecutionStatus.Closed;
            }
            catch
            {
                // An unhandled exception occured.  Throw it back to the WorkflowRuntime.
                throw;
            }
        }

        /// 
        /// Calculates the sale taxes.
        /// 
        private void SendEmailNotification()
        {
            //provide implementation here
        }

        /// 
        /// Validates the runtime.
        /// 
        /// 
        private bool ValidateRuntime()
        {
            // Create a new collection for storing the validation errors
            ValidationErrorCollection validationErrors = new ValidationErrorCollection();

            // Validate the Order Properties
            this.ValidateOrderProperties(validationErrors);

            // Raise an exception if we have ValidationErrors
            if (validationErrors.HasErrors)
            {
                string validationErrorsMessage = String.Empty;

                foreach (ValidationError error in validationErrors)
                {
                    validationErrorsMessage +=
                        string.Format("Validation Error:  Number {0} - '{1}' \n",
                        error.ErrorNumber, error.ErrorText);
                }

                // Throw a new exception with the validation errors.
                throw new WorkflowValidationFailedException(validationErrorsMessage, validationErrors);

            }


            // If we made it this far, then the data must be valid.
            return true;
        }

        private void ValidateOrderProperties(ValidationErrorCollection validationErrors)
        {
            // Validate the To property
            if (this.OrderGroup == null)
            {
                ValidationError validationError = ValidationError.GetNotSetValidationError("OrderGroup");
                validationErrors.Add(validationError);
            }
        }
    }
}

Example: CustomCartCheckout.cs.

C#
using Mediachase.Commerce.Engine;
using Mediachase.Commerce.Workflow.Activities;
using Mediachase.Commerce.Workflow.Activities.Cart;

namespace CustomActivity
{
    [ActivityFlowConfiguration(Name = "CartCheckout")]
    public class CustomCartCheckout : ActivityFlow
    {
        public override ActivityFlowRunner Configure(ActivityFlowRunner runner)
        {
            return runner.If(() => ShouldProcessPayment())
                               .Do<ProcessPaymentActivity>()
                         .EndIf()
                         .Do<CalculateTotalsActivity>()
                         .Do<AdjustInventoryActivity>()
                         .Do<RecordPromotionUsageActivity>()
                         .Do<SendNotification>();
        }
    }
}

Do you find this information helpful? Please log in to provide feedback.

Last updated: Oct 12, 2015

Recommended reading