If you are a Sitecore CMS developer and you have to create a new action for engagement plan in this is the right place to be.

Preconditions

Create new Engagement Plan

Lets at first create a new engagement plan. We will need it for further testing. Go to Marketing Control Panel and add a new engagement plan.
 
NewEngagementPlan

New engagement plan.

 

Now, let's start with some code...

At first, I used DotPeek to decompile Sitecore.Shell.MarketingAutomation.dll and I have found AddPoint class.
 
Decompiled AddPointsAction class

Decompiled AddPointsAction class

 
I gonna use AddPoint(which represents one of the predefined actions) to be my template for the new action. I created CreateItemWithTimestampAction since it is going to create an item with timestamp everytime the action is triggered.
 
using Sitecore;
using Sitecore.Analytics.Automation;
using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.SecurityModel;

namespace SitecoreKickOff.Actions
{
    public class CreateItemWithTimestampAction : IAutomationAction
    {
        public AutomationActionResult Execute(AutomationActionContext context)
        {
            string itemName = context.Parameters["ItemName"];
            string itemTemplate = context.Parameters["ItemTemplateId"];
            string itemParent = context.Parameters["ItemParentId"];

            if (itemName == null || itemTemplate == null || itemParent == null)
            {
                return AutomationActionResult.Continue;
            }

            CreateItem(itemName, itemParent, itemTemplate);

            return AutomationActionResult.Continue;
        }

        private void CreateItem(string itemName, string parentId, string templateID)
        {
            Database masterDB = Factory.GetDatabase("master");
            Item parentItem = masterDB.GetItem(new ID(parentId));

            string name = itemName + DateUtil.IsoNow;
            var template = masterDB.GetTemplate(new ID(templateID));

            using (new SecurityDisabler())
            {
                Item newItem = parentItem.Add(name, template);

                if (newItem != null)
                {
                    newItem.Editing.BeginEdit();
                    //Edit your item
                    newItem.Editing.EndEdit();
                }
            }
        }
    }
}
Now we have to create a code for the editor. Again I used DotPeek to decompile the dll and find AddPoint class in ActionEditors namespace which we will use as a template for my new CreateItemWithTimestampActionEditor class.
 
Decompiled AddPoint editor class

Decompiled AddPoint editor class

 
Here you have my new class representing the editor.
 
using System;
using System.Web.UI.WebControls;
using Sitecore.Globalization;
using Sitecore.Shell.Applications.MarketingAutomation.Dialogs;

namespace SitecoreKickOff.Actions
{
    public class CreateItemWithTimestampActionEditor : EditorBase
    {
        private const string ItemName = "ItemName";
        private const string ItemTemplateId = "ItemTemplateId";
        private const string ItemParentId = "ItemParentId";

        protected TextBox ItemNameTextBox;
        protected Literal ItemNameLiteral;

        protected TextBox TemplateIdTextBox;
        protected Literal TemplateIdLiteral;

        protected TextBox ParentIdTextBox;
        protected Literal ParentIdLiteral;

        protected override void ConfigureSaveParameters()
        {
            SetParameterValue(ItemName, ItemNameTextBox.Text);
            SetParameterValue(ItemTemplateId, TemplateIdTextBox.Text);
            SetParameterValue(ItemParentId, ParentIdTextBox.Text);
        }

        private void InitValues()
        {
            ItemNameTextBox.Text = GetParameterValueByKey(ItemName, string.Empty);
            TemplateIdTextBox.Text = GetParameterValueByKey(ItemTemplateId, string.Empty);
            ParentIdTextBox.Text = GetParameterValueByKey(ItemParentId, string.Empty);
        }

        protected override void Localize()
        {
            base.Localize();
            ItemNameLiteral.Text = Translate.Text(ItemNameLiteral.Text);
            TemplateIdLiteral.Text = Translate.Text(TemplateIdLiteral.Text);
            ParentIdLiteral.Text = Translate.Text(ParentIdLiteral.Text);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!Page.IsPostBack)
            {
                InitValues();
            }
        }
    }
}

Let's define UI for the editor

Go to webroot/hostname/Website/sitecore/shell/Applications/MarketingAutomation/Dialog/AcionEditors.This path contains UI files for the editors. Pro tip: the format has to be .xaml.aspx, so just copy and paste the AddPoint file and rename it.

Folder containing UI for action editors

Folder containing UI for action editors

Here you have UI for my editor class. Another pro tip: remember to have the same namespace as our editor class.
 
<?xml version="1.0" encoding="UTF-8" ?>
<xamlControls
  xmlns:x="http://www.sitecore.net/xaml"
  xmlns:ajax="http://www.sitecore.net/ajax"
  xmlns:rest="http://www.sitecore.net/rest"
  xmlns:javascript="http://www.sitecore.net/javascript"
  xmlns:r="http://www.sitecore.net/renderings"
  xmlns:xmlcontrol="http://www.sitecore.net/xmlcontrols"
  xmlns:p="http://schemas.sitecore.net/Visual-Studio-Intellisense"
  xmlns:asp="http://www.sitecore.net/microsoft/webcontrols"
  xmlns:html="http://www.sitecore.net/microsoft/htmlcontrols"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <SitecoreKickOff.Actions.CreateItemWithTimestampActionEditor
      x:inherits="SitecoreKickOff.Actions.CreateItemWithTimestampActionEditor, SitecoreKickOff">

    <Sitecore.Controls.DialogPage Icon="Software/32x32/graph_edge_directed.png" runat="server"  
                                  Header="Create an Item with timestamp"
                                  Text="Set items name, template and its parent item." >
      
      <AjaxScriptManager runat="server" />
      <ContinuationManager runat="server" />
      <Border runat="server"  Margin="20 0 10 0" >
        <table width="100%">
          <tr>
            <td>
              <asp:Literal ID="ItemNameLiteral" runat="server" Text="Item's Name:" />
            </td>
            </tr>
          <tr>
            <td>
              <asp:TextBox ID="ItemNameTextBox" runat="server"  Width="100%" Text="0" onFocus="javascript:this.select();"/>
            </td>
          </tr>
          <tr>
            <td>
              <asp:Literal ID="TemplateIdLiteral" runat="server" Text="Items's template ID:" />
            </td>
          </tr>
          <tr>
            <td>
              <asp:TextBox ID="TemplateIdTextBox" runat="server"  Width="100%" Text="0" onFocus="javascript:this.select();"/>
            </td>
          </tr>
          <tr>
            <td>
              <asp:Literal ID="ParentIdLiteral" runat="server" Text="Parent Item ID:" />
            </td>
          </tr>
          <tr>
            <td>
              <asp:TextBox ID="ParentIdTextBox" runat="server"  Width="100%" Text="0" onFocus="javascript:this.select();"/>
            </td>
          </tr>
        </table>      
      </Border>
      <div>
        <Literal ID="SelectSate" Text="State:"/>
        <div style="padding-top:5px; position: relative;">
          <GroupedList ID = "StateList" runat="server"
                       ParentItemId="4497A9BE-0A00-4EE4-9AE0-50D6CBB4FAB2"
                       TemplateGroupId="6E5B63D6-2401-4A52-8B4D-CFEF5E4E9752"
                       TemplateItemId="8CE2707A-3742-4A89-933B-065E5BE02BC9"
                       Height="400px"/>
        </div>
      </div>
   
    </Sitecore.Controls.DialogPage>
  </SitecoreKickOff.Actions.CreateItemWithTimestampActionEditor>
</xamlControls>

Time for some Sitecoring

Now when we have the code let's bind it to Sitecore items. Define a new Action item under /sitecore/system/Settings/Analytics/Engagement Automation/Predefined Items/Actions.

New action item in Sitecore

New action item in Sitecore

The type field represents our CreateItemWithTimestampAction. Enter the Namespace and Assembly name. The editor field represents xaml.xml file defining the editor. This time remember to use aspx format.

Testing time

Basically, that's it. Now go to Marketing Control Panel, find your engagement plan, add your new action as a condition result.
 
Adding the custom action to engagement plan

Adding the custom action to engagement plan

 
Remember to edit your action...

Editing action properties

Editing action's properties

 
  • /sitecore/content/Home {110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}
  • /sitecore/templates/Sample/Sample Item {76036F5E-CBCE-46D1-AF0A-4143F9B557AA} 
 
In order to test it go to supervisor tool, and add a new user to the plan's state and trigger the plan itself. It might take some time...
 
Hope, this was helpful, and see you next time.
Subscribe to my newsletter:


2 Comments

Dawid · October 17, 2017 at 8:45 am

Very nice and helpful article. Thank you very much!

Teres · October 18, 2017 at 7:13 pm

For me very helpful. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

meetup

SUGPL Meetup Warsaw – Video Presentation

Video presentation SUGPL Meetup Warsaw – 23 March 2018 In my last post, I have invited you all to a SUGPL Meetup Warsaw which took place on 23 March 2018. I have been one of the presenters. Read more…

meetup

SUGPL Meetup Warsaw – 23 March 2018

SUGPL Meetup Warsaw This gonna be a short post but I would like to invite you all to a SUGPL Meetup Warsaw which takes place on 23 March 2018. I will be there and I will Read more…

sitecore

Customize Rich Text Editor in Sitecore

Recently, I have come up with an idea to Customize Rich Text Editor in Sitecore by adding a button that embeds GIPHY player into my content. So, I thought that I will share this with Read more…