Umbraco events, automating the mundane!

As a developer building a site, you have an intimate knowledge of how all the content fits together, what needs to be created where and often if something breaks that dreaded lightbulb in your head pops on with an “Oh dear i forgot that null check” so you know what needs to be done to fix it.

As a content editor or site admin you often dont have these insights so have to rely on the developer to make the editing experience robust and intuitive and if things dont display on a site it can be difficult to troubleshoot or find out why if you arent familiar with it and cant see the code.

This is why when building Umbraco sites I always try to automate as much as possible, for example one of our sites had a Video content node, this needed two sub nodes to be able to catogorise the type of videos. We could have left this up to the content editor to create these as needed, or when displaying the content checked if the child nodes exist. I thought however this was a dead ringer for creating these nodes automatically when the Video content node was created.

The above is a simplistic example of the power of the Umbraco events but its a good one to illustrate how we can set this up and how useful they can be. You can see a full rundown of all the events and their various properties here: https://our.umbraco.org/documentation/reference/events/. The one ill be using today is the Content Saved event.

To create this event handler we need to first need to hook into the ApplicationStart event. This code should either go somewhere like in a folder in your main umbraco project or start a seperate class library project and reference that in your main umbraco project. Rememebr to reference the umbraco core/cms dlls in your class library if you go down this route however or you’ll just get a load of errors!

 class ApplictionEvents : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
           ContentService.Saved += ContentServiceSaved;
        }       
     }

So we have now created an event handler for the ContentService.Saved event and bound it to the ContentServiceSaved function. Now we need to write that function:

 class ApplictionEvents : ApplicationEventHandler
    {
        protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
            ContentService.Saved += ContentServiceSaved;
        }

        private void ContentServiceSaved(IContentService sender, SaveEventArgs args)
        {
            foreach (var node in args.SavedEntities)
            {
                if (node.ContentType.Alias == "video" && node.IsNewEntity())
                {
                    //create new sub categories
                    var prodResp = ApplicationContext.Current.Services.ContentService.CreateContent("Category One", node.Id, "categoryOne");
                    var usrResp = ApplicationContext.Current.Services.ContentService.CreateContent("Category Two", node.Id, "categoryTwo");

                    ApplicationContext.Current.Services.ContentService.Save(prodResp);
                    ApplicationContext.Current.Services.ContentService.Save(usrResp);
                }
            }
        }
    }

This is pretty simple but it shows how you could do all kinds of things with the events in umbraco. With this one, we loop over all the entities that are being saved, check if they are of the content type we want and if its the first time we are saving them (we dont want to create more nodes each time the item is saved) and if so we automatically create the two sub category nodes.

Hopefully this will get you started with the events, check back soon for some more examples!

back to blog

Related posts