Monday 28 June 2021

Custom Event in Sitecore

Custom Event in Sitecore

  • Events in Sitecore are similar to events in other systems: something triggers an event and there are handlers that are configured to handle the event.
  • Event handlers are similar to pipelines in how they are configured. An event handler is a .NET class that implements a method. When an event is triggered, the event handlers are run in order.
  • Event handlers are defined in Sitecore patch files. 

Adding an Event Handler


While there is neither a class to extend nor an interface to implement, a convention must be followed in order for Sitecore to be in order for a class to be used as an event handler.

The class must have a method that accepts two parameters and return void:

object - represents the object that holds a collection of the various event listeners
EventArgs - holds the parameters being passed to the event handler
The following is an example of a custom processor.

public class MyEventHandlers
{
    public void OnItemSaved(object sender, EventArgs args)
    {
        //do something
    }
}

Defining an Event


An event itself is nothing more than a block of XML in the /configuration/Sitecore/events section of a Sitecore patch file.

The following is an example of a custom event definition.

<testing:myevent>
  <processor type="Testing.Events.SetVal1, Testing" />
  <processor type="Testing.Events.SetVal2, Testing" />
</testing:myevent>


Raising an Event


The following is an example of raising an event.

public class EventRaiser
{
  public void RaiseEvent()
  {
    var args = new Testing.Events.MyEventArgs();
    Sitecore.Events.Event.RaiseEvent("testing:myevent", args);
  }
}

Example of custom Event:


using System;
using Sitecore.Pipelines;

namespace SitecoreLearning.Helpers.CustomEvent
{
    /*A custom EventArgs class makes it easier to pass objects between event handlers and to provide output to the process that triggered the event. 
     At runtime, the EventArgs object acts as the event’s context.
     create a custom EventArgs class you must inherit from System.EventArgs. */
    public class EventRemote: System.EventArgs
    {
        public string ItemId { get; set; }
    }

    public class EventHandlers
    {
        public virtual void InitializeFromPipeline(PipelineArgs args)
        {
            Sitecore.Diagnostics.Log.Info("EventHandlers.InitializeFromPipeline() called", this);
            var action = new Action<EventRemote>(RaiseRemoteEvent);
            Sitecore.Eventing.EventManager.Subscribe<EventRemote>(action);
        }
        private void RaiseRemoteEvent(EventRemote myEvent)
        {
            Sitecore.Diagnostics.Log.Info("EventHandlers.RaiseRemoteEvent() called", this);
            Sitecore.Events.Event.RaiseEvent("event:happened:remote", new object[] { myEvent.ItemId });
        }
    }

    public class EventManager
    {
        /*
         Adding an Entry to the Event Queue, Next, you need to add an entry to the event queue. 
         Often it makes sense to trigger the local event and to add the remote event to the event queue at the same time. 
         The following is an example of this.
        */
        public static void RaiseEventRemote()
        {
            Sitecore.Diagnostics.Log.Info("EventManager.RaiseEventRemote() called", typeof(EventManager));
            var parameters = new object[] { new Guid().ToString() };
            Sitecore.Events.Event.RaiseEvent("event:happened", parameters);
        }
        public void OnEvent(object sender, EventArgs args)
        {
            Sitecore.Diagnostics.Log.Info("EventManager.OnEvent() called", this);
        }
        public void OnEventRemote(object sender, EventArgs args)
        {
            Sitecore.Diagnostics.Log.Info("EventManager.OnEventRemote() called", this);
        }
    }
}

Note: call the static method in your business logic then the event handler will be called automatically.

Defined in Sitecore patch files:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <initialize>
        <processor type="SitecoreLearning.Helpers.CustomEvent.EventHandlers, SitecoreLearning" method="InitializeFromPipeline" />
      </initialize>
    </pipelines>
    <events>
      <!--Start custom Events -->
      <event name="event:happened">
        <handler type="SitecoreLearning.Helpers.CustomEvent.EventManager, SitecoreLearning" method="OnEvent" />
      </event>
      <event name="event:happened:remote">
        <handler type="SitecoreLearning.Helpers.CustomEvent.EventManager, SitecoreLearning" method="OnEventRemote" />
      </event>
      <!--End custom Events -->
    </events>
  </sitecore>
</configuration>

Sitecore Publishing Service 7.0 Installation Guide

  About the Publishing Service module The Publishing Service module is an optional replacement for the existing Sitecore publishing methods....