Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • ssougnez 93 posts 319 karma points c-trib
    Nov 10, 2018 @ 20:41
    ssougnez
    0

    Handling events differently

    Hi,

    recently, I had to create a site with Umbraco and I ended up in a situation where I had multiple document type event handlers. However, as the events of "ContentService" are centralized, I had to use multiple "if" to check the document type of the current event and then execute the correct instructions based on the condition.

    Therefore, I decided to create a small package that would allow me to create classes that handle specific document type events. Long story short, you can find such a class below:

    [DocumentEventHandler(Include = new[] { "documentTypeA" })]
    public class DocumentTypeAEventHandler : DocumentEventHandler
    {
        public override void Saving(IContentService contentService, SaveEventArgs<IContent> args)
        {
            base.Saving(contentService, args);
        }
    }
    

    By using the DocumentEventHandler attribute, I'm able to narrow the overridden methods (Saving, Publishing, etc...) to be executed only for a specific document type. It is possible to use "Include" and "Exclude" properties to handle more than one document type.

    More information can be found on the package page.

    Don't hesitate to tell me what you think about it ;-)

Please Sign in or register to post replies

Write your reply to:

Draft