How to add custom Event handler in Sitecore?

We had a requirement of pushing some Sitecore items to third-party applications once those are publish on the Live (Web database).

We had to attach a custom event handler in the Publishing event. Now the handler needs to be added in both publish:end and publish:end:remote, the former works well on single instances and CM server and later is for the Production server setups where your CD servers are different.

Adding custom event handler takes only 2 steps.

  1. Add a class with the business logic.
  2. Add a config file to patch in this class in the events.
  1. Add a class
    Create a class file which looks like below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Events;
using MOIAT.Foundation.SitecoreExtensions;

namespace Sitecore.Foundation.APIIntegrations.Helpers
{
    public class PublishConsultations
    {
        public void PublishConsultation(object sender, EventArgs e)
        {
            if(e == null)
            {
                Sitecore.Diagnostics.Log.Error("Consultation publishing failed", this);
                return;
            }

            Sitecore.Publishing.Publisher publishingOptions = null;

            if (e != null)
            {
                publishingOptions = Event.ExtractParameter(e, 0) as Sitecore.Publishing.Publisher;
            }

            if(publishingOptions == null || publishingOptions.Options == null)
            {
                return;
            }

            var item = publishingOptions.Options.RootItem;

            if ((object)item == null)
            {
                return;
            }

            if(item.TemplateID.ToString() == "{2771C35C-0FE9-4E02-A85C-D4347B0EA0A4}")
            {
                //Do some business logic
            }

            Sitecore.Diagnostics.Log.Info("The publishing event is triggered.", this);
        }
    }
}

2. Add a patch file which should look like below:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
  <sitecore>
	  <events>
		  <event name="publish:end">
			  <handler type="Sitecore.Foundation.APIIntegrations.Helpers.PublishConsultations, Sitecore.Foundation.APIIntegrations" method="PublishConsultation" />
		  </event>
		  <event name="publish:end:remote">
			  <handler type="Sitecore.Foundation.APIIntegrations.Helpers.PublishConsultations, Sitecore.Foundation.APIIntegrations" method="PublishConsultation" />
		  </event>
	  </events>
  </sitecore>
</configuration>

Publish this project and you are done, every time you publish items, this handler will be called so write the code wisely.

Thank you.. Keep Learning.. Keep Sitecoring.. šŸ™‚

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s