Skip to main content
1 of 3
Matthew Dresser
  • 6.1k
  • 2
  • 29
  • 77

How to raise an event in Sitecore 9+

In Sitecore 9 the event model has changed somewhat and events are now queued within each DB (master, core, web etc).

Previously in Sitecore, we would use code like this to raise an event:

var args = new Testing.Events.MyEventArgs();
Sitecore.Events.Event.RaiseEvent("testing:myevent", args);

Note how the name of the event is passed ("testing:myevent") and then some args which I believe are generic event args which are then cast back to a specific EventArg type within the handling event.

In Sitecore 9 and later, we would use something like below

var database = Sitecore.Configuration.Factory.GetDatabase("web");
var eventQueue = database.RemoteEvents.EventQueue;
eventQueue.QueueEvent(someObject);

I'm confused as to what/how to create the object someObject. The QueueEvent method has two signatures:

QueueEvent(RemoteEventArgs<TEvent> event)
QueueEvent(RemoteEventArgs<TEvent> event, bool addToGlobalQueue, bool addToLocalQueue)

Say I want to raise the event publish:end:remote to trigger a cache clear, I look at the configs and see a load of different handlers for this event (I have SXA and Commerce installed) but looking at the OOTB one which is Sitecore.Publishing.HtmlCacheClearer within Sitecore.Kernel.dll it has a method ClearCache(object sender, EventArgs e) i.e. just a generic EventArgs parameter.

I don't understand how the event object passed to QueueEvent identifies the name of the event. I also don't see how the event could be identified magically by passing in a specific event arg type.

Matthew Dresser
  • 6.1k
  • 2
  • 29
  • 77