Brian McKeiver's Blog

Kentico 8 Marketing Automation Sample Code Snippets


Introduction

Recently I was tasked with creating my own On-line Marketing Automation Process Action inside of Kentico 8. I wasn't exactly sure of which method signature to start from so it took a bit of digging to figure out the right class to inherit from. Luckily it didn't take long because the excellent help documentation that comes with Kentico 8 set me on the right path. I'm hoping to save the next Kentico developer who has to do this some time, so I have posted two sample code snippets below. Have I mentioned that I love how easy it is to extend the built-in foundation of Kentico ?

 

Custom Marketing Automation Actions

The sample code below contains a sample class for both of the custom Action types that the Kentico 8 API offers. One for a general use AutomationAction and one for a ContactAutomationAction that allows your code to work with the current On-line Marketing Contact that is being processed in the Process.

 

using System;
using CMS;
using CMS.Automation;
using CMS.OnlineMarketing;
 
/// 
/// Custom Class for ContactAutomationAction as a Marketing Automation Process Action. Use for actions 
/// that work with the contacts being handled by the process. Full documentation:
/// https://docs.kentico.com/display/K8/Developing+custom+marketing+automation+actions
/// 
[assemblyRegisterCustomClass("MySampleContactAutomationAction"typeof(MySampleContactAutomationAction))]
 
public class MySampleContactAutomationAction : ContactAutomationAction
{
    private string actionParam1 = null;
 
    /// 
    /// Parameter from the Process that it is configured to use
    /// 
    public string ActionParam1
    {
        get
        {
            if (actionParam1 == null)
            {
                //ActionParam1 is the name of the Parameter
                actionParam1 = GetResolvedParameter<string>("ActionParam1"string.Empty);
            }
 
            return actionParam1;
        }
    }
 
    /// 
    /// Executes the custom logic for the ContactAutomationAction
    /// 
    public override void Execute()
    {
        if (Contact != null)
        {
            // You can use ActionParam1 however you wish here. You
            // can also use or update the Current Contact that is 
            // running through the Marketing Automation Process here.
            Contact.ContactNotes = string.Format("Updated on: {0}", DateTime.Now);
            ContactInfoProvider.SetContactInfo(Contact);
        }
        
    }
}
 
 
 
/// 
/// Custom Class for AutomationAction as a Marketing Automation Process Action.
/// Use for general automation actions. Full documentation:
/// https://docs.kentico.com/display/K8/Developing+custom+marketing+automation+actions
/// 
[assemblyRegisterCustomClass("MySampleAutomationAction"typeof(MySampleAutomationAction))]
 
public class MySampleAutomationAction : AutomationAction
{
    private string actionParam1 = null;
 
    /// 
    /// Parameter from the Process that it is configured to use
    /// 
    public string ActionParam1
    {
        get
        {
            if (actionParam1 == null)
            {
                //ActionParam1 is the name of the Parameter
                actionParam1 = GetResolvedParameter<string>("ActionParam1"string.Empty);
            }
 
            return actionParam1;
        }
    }
 
    /// 
    /// Executes the custom logic for the AutomationAction
    /// 
    public override void Execute()
    {
        // You can use ActionParam1 however you wish here.
 
    }
}

 

Registering Your Custom Action

After you code is done, your next step to use it is to register the Action inside of the Kentico Admin interface. When you are on the New Action screen you should see your custom class as an option under the Action configuration group. If you don't see it you missed something.

 

Kentico Marketing Automation New Action

 

Also don't forget to set a parameter if you need one.

 

Marketing Automation Action Parameter

 

What It Looks Like 

If everything worked you should now have your own Action in the Visual Designer.

 

Kentico 8 Marketing Automation Custom Action

 

Conclusion

Hopefully this sample helps out the next time you want to add your own Action steps in Kentico's Integrated On-line Marketing Solution.