Customizing Sitecore Webhooks – With Custom Event and Custom Payload

Was asked in Sitecore Slack recently whether we can customize Sitecore Webhook Payload. I didn’t know if we could or how to customize them. Wanted to find an answer to that…..Found a way, thus this article.

Please note that as I did this just for exploring and self learning or as a POC kind of activity, I couldn’t completely develop this as a enterprise level solution to a requirement. Please update the code as per your need/coding standards.

Let’s get started.

Sitecore Webhook Payloads are way more Sitecore Centric and are (as it is) at times not fit to be posted to external systems.

When I explored Sitecore Webhook initially when it was released felt that the default payload is more Sitecore Centric meaning, it has only the Item ID, Publish Target, RecoveryID etc ???? PFB a sample payload for an Publish End event. I am not sure if all these fields are useful or even required while posting a payload to different system.

In order to make full use of Webhooks, this payload data must be customizable.

Say we need some configuration item belonging to a particular template whenever created and published, some of it’s field values needs to be posted to an external system. Or after a Sitecore Task Scheduler is successful and you have to post an item’s field value to an external system. These are scenarios when Sitecore Webhook come handy. But with OOTB implementation we may not be able to post those item’s field data or any other computed data. In such cases, customizing the webhook payload will be handy.

Initially, tried going through the \App_Config\Sitecore\CMS.Core\Sitecore.Webhooks.config and checked WebhookSubscriber and InitializeWebhooks from Kernel DLL. But didn’t find any useful.

When went through the Webhook Event Item, saw that it didn’t have any field except Event Name.

Just took a shot, thought of creating a custom event and triggering the event with custom properties hoping that will be used as payload when the event is triggered.

Step 1: Created your custom event and it’s event handler.

P.S: Here, instead of creating a custom event, you can create a event that subscribes to existing sitecore system events. So that these events will always be called after those system events. Custom properties can be added to these new events which will be used by Sitecore Webhook for payload.

Step 2: Create a Custom Folder inside Webhooks/Event Types. (So that you have a separation between sitecore system events and custom events)

Step 3: Trigger this event with Custom Payload. I just created a Controller Rendering with an action that triggers the event with custom payload.

Created properties – a dictionary with string as key and object as value. Included the custom payload.

(Webhooks weren’t triggering when you don’t use properties meaning payload which is not of Dictionary<string, object>. I got error initially and spent a lot of time troubleshooting when I used Event.RaiseEvent(events, new object[]{ “key”, “value” });)

Step 4: Create a Sitecore Webhook Handler that subscribes to our custom event.

Included this to a page for testing and loaded the page……Voila!

While exploring about Customizing Sitecore Webhook payload, I came across this article by Kristoffer Brinch Kjeldby and found it helpful. Please refer them if you want to customize the payload for any existing events.

One thought on “Customizing Sitecore Webhooks – With Custom Event and Custom Payload

Leave a comment

Design a site like this with WordPress.com
Get started