Try our conversational search powered by Generative AI!

K Khan
Nov 12, 2015
  9648
(2 votes)

WebHooks Sender and Receiver

Microsoft ASP.NET WebHooks makes it easier to both send and receive WebHooks as part of your an ASP.Net application so for an EPiServer application also. I have been given a chance for a technical talk with fellow EMVP Mark Everard at EPiServer UK Ascend. I have presented a demo Server that was responsible to subscribe alloy site and publish an event. On other hand Alloy site was accepting those changes via Microsoft ASP.Net WebHooks.

On the receiving side, ASP.NET WebHooks provides a common model for receiving and processing WebHooks from any number of WebHook providers. In my case Alloy site will be a receiving side processing a change of display name received from a Demo server.

On the sending side, ASP.NET WebHooks provides support for managing and storing subscriptions as well as for sending event notifications to the right set of subscribers. In my case Demo server will send notification to (subscriber ) alloy site for defined set of event 'entryupdating'.

A console demo server application can be downloaded, that will subscribe alloy site and will publish an event that a display name has been changed for a product.

To set-up a receiver, you will require some exercise.

Download and set-up Alloy site.

Install a custom webhook receiver Nuget Package 

Initialize the Custom WebHook Receiver

public static void Register(HttpConfiguration config)
        {
            // Web API routes
          
            // Initialize Custom WebHooks Register
            config.InitializeReceiveCustomWebHooks();
        }

Add following application settings in Web.config

<appSettings>
  <add key="MS_WebHookReceiverSecret_Custom" value="12345678901234567890123456789012" />
</appSettings>

Add a handler, that will receive changes

public class Handler : WebHookHandler
    {
        public Handler()
        {
            this.Receiver = "custom";
        }

        public override Task ExecuteAsync(string receiver, WebHookHandlerContext context)
        {
            JObject entry = context.GetDataOrDefault<JObject>();
            CustomNotification notification = entry["Notifications"][0].ToObject<CustomNotification>();
            if (notification.Action == "entryupdating")
            {
                UrlResolver resolver = ServiceLocator.Current.GetInstance<UrlResolver>();
                var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
                var product = resolver.Route(new EPiServer.UrlBuilder(string.Format("http://localhost:51481/en/{0}/", notification.ProductId)));
                var page = contentRepository.Get<ProductPage>(product.ContentLink).CreateWritableClone();

                if (page != null)
                {
                    page.Name = notification.ProductName;
                }
                contentRepository.Save(page, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);
            }
            return Task.FromResult(true);   
        }
    }

In case if you will be wondering what is CustomNotification

public class CustomNotification
    {
        [JsonProperty("Action")]
        public string Action { get; set; }

        [JsonProperty("id")]
        public string ProductId { get; set; }
        
        [JsonProperty("name")]
        public string ProductName { get; set; }
    }

How To Run:

Run Alloy Site (in debug mode to see what WebHookHandlerContext contains)

 Image alloy0.JPG

Run Console application (It will subscribe alloy in memory of console application and will publish change of display name)

Refresh your browser
Image alloy.JPG

(Don't ask! can we not do this using CMS Online center :) )

You will have to bear with me for few more blogs on this topic.

Happy Hooks!

Nov 12, 2015

Comments

Per Magne Skuseth
Per Magne Skuseth Nov 13, 2015 10:40 AM

Cool!

K Khan
K Khan Nov 13, 2015 11:48 AM

Its a food of thought for system architecths. They can think outside of the boundries with this option. It can help a lot in Commerce Projects where multiple systems and technologies are working togather. e.g. in Pricing or inventory updates, Order fulfilments etc.

Please login to comment.
Latest blogs
Optimizely Forms - How to add extra data automatically into submission

Some words about Optimizely Forms Optimizely Forms is a built-in add-on by Optimizely development team that enables to create forms dynamically via...

Binh Nguyen Thi | Apr 29, 2024

Azure AI Language – Extractive Summarisation in Optimizely CMS

In this article, I demonstrate how extractive summarisation, provided by the Azure AI Language platform, can be leveraged to produce a set of summa...

Anil Patel | Apr 26, 2024 | Syndicated blog

Optimizely Unit Testing Using CmsContentScaffolding Package

Introduction Unit tests shouldn't be created just for business logic, but also for the content and rules defined for content creation (available...

MilosR | Apr 26, 2024

Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog