Umbraco backend customization - handling events

Hook on umbraco events and control them from your code

In Umbraco 6 new interface IApplicationHandler is introduced. By inheriting this interface in your class you can hook your custom methods to handle events which occur in Umbraco.

The following two I use really often for controlling Umbraco behavior:

  • MediaService
  • ContentService

Beside these two, you can find others which can help you controlling Umbraco events but I barely use them at anytime. Anyhow, it is good to know that you have them.

Note

This article is related and tested on Umbraco v6.1.6 (Assembly version: 1.0.5021.24867). I am not sure that the same approach can be applied to Umbraco 7

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Umbraco.Core;
using Umbraco.Core.Services;

namespace Umbraco.Cms.Custom
{
    public class UmbracoEventHandlers : ApplicationEventHandler
    {
        public UmbracoEventHandlers()
        {
            MediaService.Saving += new Core.Events.TypedEventHandler<IMediaService, Core.Events.SaveEventArgs<Core.Models.IMedia>>(MediaService_Saving);
            ContentService.Publishing += new Core.Events.TypedEventHandler<Core.Publishing.IPublishingStrategy, Core.Events.PublishEventArgs<Core.Models.IContent>>(ContentService_Publishing);
            FileService.SavedTemplate += new Core.Events.TypedEventHandler<IFileService, Core.Events.SaveEventArgs<Core.Models.ITemplate>>(FileService_SavedTemplate);
            LocalizationService.SavedLanguage += new Core.Events.TypedEventHandler<ILocalizationService, Core.Events.SaveEventArgs<Core.Models.ILanguage>>(LocalizationService_SavedLanguage);
        }

        void LocalizationService_SavedLanguage(ILocalizationService sender, Core.Events.SaveEventArgs<Core.Models.ILanguage> e)
        {
            throw new NotImplementedException();
        }

        void FileService_SavedTemplate(IFileService sender, Core.Events.SaveEventArgs<Core.Models.ITemplate> e)
        {
            throw new NotImplementedException();
        }

        void ContentService_Publishing(Core.Publishing.IPublishingStrategy sender, Core.Events.PublishEventArgs<Core.Models.IContent> e)
        {
            throw new NotImplementedException();
        }

        void MediaService_Saving(IMediaService sender, Core.Events.SaveEventArgs<Core.Models.IMedia> e)
        {
            throw new NotImplementedException();
        }
    }
}
    

Disclaimer

Purpose of the code contained in snippets or available for download in this article is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.


About the author

DEJAN STOJANOVIC

Dejan Stojanovic is a passionate Software Architect/Developer. He is highly experienced in .NET programming platform includion ASP.NET MVC and WebApi. He likes working on new technologies and exciting challenging projects

CONNECT WITH DEJAN  Loginlinkedin Logintwitter Logingoogleplus Logingoogleplus

.NET

read more

JavaScript

read more

SQL/T-SQL

read more

PowerShell

read more

Comments for this article