Adding Webhooks to Umbraco Content Events to Trigger External Services

Today, we'll be delving into the heart of Umbraco - one of the most flexible open-source content management systems (CMS) built on Microsoft's .NET framework. Specifically, we're going to explore how you can add webhooks to Umbraco content events, allowing you to trigger external services whenever changes occur in your content. This opens a multitude of possibilities for synchronisation, automation, and enhanced interactivity across various platforms.

 

Before we get into the thick of it, make sure you have a solid understanding of Umbraco and a fair acquaintance with its APIs and content services.

 

If you're just looking to get functionalities like webhooks added to your CMS quickly by one of our in-house developers, check out our Umbraco Support page to view the contracts we offer. Alternatively, check out our Umbraco Training page to view how we can help you get the most out of your CMS and access all of it's functionalities.

What are Webhooks?

Webhooks are a way for applications to provide other applications with real-time information, thus creating a network of communicating software. They're a form of reverse API, sending out a message (payload) to a unique URL when a specific event happens.

Why Use Webhooks with Umbraco?

By using webhooks with Umbraco, you can trigger actions in external services when certain events happen in the Umbraco CMS. This adds a real-time, automated, and synchronous aspect to your CMS operations, allowing you to react to content changes almost instantly and in a programmatically defined manner.

Creating a Notification Handler for Content Published Notification

Now, let's get to the meat of the matter. How do you add webhooks to Umbraco's content events?

 

 

Step 1: Create a New Notification Handler

Start by creating a new notification handler where you will handle the ContentPublishedNotification.

csharp
public class ContentPublishedNotificationHandler : INotificationHandler<ContentPublishedNotification> { // ... }

 

Step 2: Handle the ContentPublishedNotification

Now, in your notification handler, you can handle the ContentPublishedNotification. When this notification is triggered, you will call your webhook.

csharp
public class ContentPublishedNotificationHandler : INotificationHandler<ContentPublishedNotification> { public Task Handle(ContentPublishedNotification notification, CancellationToken cancellationToken) { // Your webhook code goes here. } }

 

Step 3: Implement Your Webhook

Within the Handle method, you can then make an HTTP POST request to the URL of your webhook. Here, notification.PublishedEntities gives you access to the content that has been published.

csharp
public class ContentPublishedNotificationHandler : INotificationHandler<ContentPublishedNotification> { public async Task Handle(ContentPublishedNotification notification, CancellationToken cancellationToken) { var webhookUrl = "https://your-webhook-url.com"; var client = new HttpClient(); foreach (var publishedContent in notification.PublishedEntities) { var payload = new { id = publishedContent.Id, name = publishedContent.Name }; var jsonPayload = JsonConvert.SerializeObject(payload); var data = new StringContent(jsonPayload, Encoding.UTF8, "application/json"); var response = await client.PostAsync(webhookUrl, data); if (response.IsSuccessStatusCode) { // Webhook call was successful. } } } }

 

Remember to replace "https://your-webhook-url.com" with your actual webhook URL.

 

Step 4: Register the Notification Handler

The last step is to register the notification handler in Umbraco. This can be done in a composer, which is Umbraco's own dependency injection framework.

csharp
public class WebhooksComposer : IUserComposer { public void Compose(Composition composition) { composition.WithNotificationHandler<ContentPublishedNotificationHandler>(); } }

 

Now, your Umbraco CMS will make a webhook call to your specified URL every time a piece of content is published, utilising the newer, more flexible Content Notifications.

Conclusion

Umbraco's Content Notifications offer a modern and enhanced way to react to content events, providing a more flexible alternative to the traditional event model. By integrating webhooks with Umbraco Content Notifications, you're further enhancing the real-time capabilities and reactivity of your CMS, paving the way for a myriad of automation possibilities.

 

 

Disclaimer: The code snippets provided are meant to illustrate the process and may need further adjustments to meet specific implementation requirements. As always, thorough testing is advised before deploying any new code to a production environment.