In this tutorial, you will learn about the new way to hook into the Umbraco events handling pipeline. For example, there are many reasons why you might want to do this. It is very common to need some additional validation to trigger before a content editor can save a page. This type of feature can be introduced by hooking into one of the page events. Umbraco comes with lots of events like 'onsave'. A full list of the events for content can be found here.
In Umbraco V8 the concept of components was introduced. Components are the new way of hooking into Umbraco. Although components can be used for numerous purposes, the focus of this article is event handling. We will focus on how to create and register a component specifically for event handling. Components are the new way to hook into the Umbraco pipeline and register your own custom functionality. There are two parts to creating a custom component:
Creating The Component
Registering The Component With Umbraco
Registering The Component With Umbraco
Registering a component is done using a composer. Create a new class that inherits fromComponentComposer
where T
is the type-definition of the custom component we will create shortly. This class can live anywhere in your solution:
When Umbraco loads it will use reflection to find all classes that inherit from ComponentComposer
and register it. The next step is to create a component. The component class is the place where you will add the code that hooks into the Umbraco event. The code to create a component is shown below:
The code above uses the save
event. Any code that is added within ContentService_Saving
will be called whenever a content editor clicks the save button within the CMS. Asides from hooking into the save
event, there are a number of other events that you can hook into, these include:
The code to hook into any of these events is the same as the example above. Register the handler and implement its event handler method, add some code, job done! There are three types of events you can hook into ContentService Events, MediaService Events, and MemberService Events. Enjoy!