x First time here? Check out the FAQ

Using ApplicationBase to register events

    Umbraco 4 includes the ApplicationBase class which is used for registering events in umbraco automatically when umbraco loads.

    This sample shows how to setup an ApplicationBase and make it subscribe to the before publishing events.

    Remember to add the cms.dll, businesslogic.dll, umbraco.dll and interfaces.dll to your project.

    Add references to the right namespaces at the top of your .cs file

    using umbraco.BusinessLogic;
    using umbraco.cms.businesslogic;
    using umbraco.cms.businesslogic.web;

    Use the ApplicationBase and place the event code in the default class constructor

    public class AppBase : umbraco.BusinessLogic.ApplicationBase {

        public AppBase() {
            Document.BeforePublish += new Document.PublishEventHandler(Document_BeforePublish);
        }

        void Document_BeforePublish(Document sender, PublishEventArgs e) {
       
            Log.Add(LogTypes.Debug, sender.Id, "the document " + sender.Text + " is about to be published");
       
            //cancel the publishing
            e.Cancel = true;
        }
    }