THE UMBRACO EVENT MODEL

The event model that Umbraco employs is great for interacting with content and media and intercepting or injecting actions into the workflow as Umbraco's node lifecycle is executed. Again, the aim of this book is to introduce you to the concepts and not delve into granular details. In short, having access to these event hooks provides you with the ability to add workflows and integrate third-party and a host of other applications.

Event Hooks

Every action in the Umbraco process is covered by the event model, which makes subscribing to any of these events with just a few lines of code possible. Table 12-1 lists all the available event hooks and in what context they are relevant. This is a listing of all the events that you can tap into. For more specific examples of when these are used, see the sample code in the “Event Samples” section.

TABLE 12-1: Event Hooks

EVENT CONTEXT EVENT NAME
Access
umbraco.cms.businesslogic.web.Access
  • BeforeSave
  • AfterSave
  • New
  • BeforeAddProtection
  • AfterAddProtection
  • BeforeRemoveProtection
  • AfterRemoveProtection
  • BeforeAddMemberShipRoleToDocument
  • AfterAddMemberShipRoleToDocument
  • BeforeRemoveMemberShipRoleToDocument
  • AfterRemoveMemberShipRoleToDocument
  • BeforeRemoveMembershipUserFromDocument
  • AfterRemoveMembershipUserFromDocument
  • BeforeAddMembershipUserToDocument
  • AfterAddMembershipUserToDocument
BaseTree
umbraco.cms.presentation.Trees.BaseTree
  • BeforeNodeRender
  • AfterNodeRender
CMSNode
umbraco.cms.businesslogic.CMSNode
  • BeforeSave
  • AfterSave
  • New
  • BeforeDelete
  • AfterDelete
  • BeforeMove
  • AfterMove
CreatedPackage
umbraco.cms.businesslogic.packager.CreatedPackage
  • BeforeSave
  • AfterSave
  • New
  • BeforeDelete
  • AfterDelete
  • BeforePublish
  • AfterPublish
Content
umbraco.content
  • BeforeUpdateDocumentCache
  • AfterUpdateDocumentCache
  • BeforeClearDocumentCache
  • AfterClearDocumentCache
  • BeforeRefreshContent
  • AfterRefreshContent
ContentControl
umbraco.controls.ContentControl
  • BeforeContentControlLoad
  • AfterContentControlLoad
DataTypeDefinition
umbraco.cms.businesslogic.datatype.DataTypeDefinition
  • Saving
  • New
  • Deleting
Dictionary
umbraco.cms.businesslogic.Dictionary
  • Saving
  • New
  • Deleting
Document
umbraco.cms.businesslogic.web.Document
  • BeforeSave
  • AfterSave
  • New
  • BeforeDelete
  • AfterDelete
  • BeforeMoveToTrash
  • AfterMoveToTrash
  • BeforePublish
  • AfterPublish
  • BeforeSendToPublish
  • AfterSendToPublish
  • BeforeUnPublish
  • AfterUnPublish
  • BeforeCopy
  • AfterCopy
  • BeforeRollBack
  • AfterRollBack
  • BeforeAddToIndex
  • AfterAddToIndex
DocumentType
umbraco.cms.businesslogic.web.DocumentType
  • BeforeSave
  • AfterSave
  • New
  • BeforeDelete
  • AfterDelete
Domain
umbraco.cms.businesslogic.web.Domain
  • BeforeSave
  • AfterSave
  • New
  • BeforeDelete
  • AfterDelete
InstalledPackage
umbraco.cms.businesslogic.packager.InstalledPackage
  • BeforeSave
  • AfterSave
  • New
  • BeforeDelete
  • AfterDelete
Macro
umbraco.cms.businesslogic.macro.Macro
  • BeforeSave
  • AfterSave
  • New
  • BeforeDelete
  • AfterDelete
Media
umbraco.cms.businesslogic.media.Media
  • BeforeSave
  • AfterSave
  • New
  • BeforeDelete
  • AfterDelete
MediaType
umbraco.cms.businesslogic.media.MediaType
  • BeforeSave
  • AfterSave
  • New
  • BeforeDelete
  • AfterDelete
Member
umbraco.cms.businesslogic.member.Member
  • BeforeSave
  • AfterSave
  • New
  • BeforeAddGroup
  • AfterAddGroup
  • BeforeRemoveGroup
  • AfterRemoveGroup
  • BeforeAddToCache
  • AfterAddToCache
  • BeforeDelete
  • AfterDelete
MemberGroup
umbraco.cms.businesslogic.member.MemberGroup
  • BeforeSave
  • AfterSave
  • New
  • BeforeDelete
  • AfterDelete
MemberType
umbraco.cms.businesslogic.member.MemberType
  • BeforeSave
  • AfterSave
  • New
  • BeforeDelete
  • AfterDelete
Language
umbraco.cms.businesslogic.language.Language
  • BeforeSave
  • AfterSave
  • New
  • BeforeDelete
  • AfterDelete
Stylesheet
umbraco.cms.businesslogic.web.StyleSheet
  • BeforeSave
  • AfterSave
  • New
  • BeforeDelete
  • AfterDelete
StylesheetProperty
umbraco.cms.businesslogic.web.StyleSheetProperty
  • BeforeSave
  • AfterSave
  • New
  • BeforeDelete
  • AfterDelete
Template
umbraco.cms.businesslogic.template.Template
  • BeforeSave
  • AfterSave
  • New
  • BeforeDelete
  • AfterDelete
User
umbraco.BusinessLogic.template.Template
  • Saving
  • New
  • Disabling
  • Deleting
  • FlushingFromCache

Event Examples

Hooking into an event requires you to simply create a class that derives from the umbraco.BusinessLogic.ApplicationBase class. Both the class and constructor must be public in order for the hook to be caught in the execution of the action.

Listing 12-17 provides an example of auto expiring a node on a particular date. This is useful for things such as news items, events, and other time-sensitive material. For the purposes of this example, assume that there's a business case to expire your FAQs after seven days from the original publish date.

image Because code that's executed in an event hook is silent (meaning it's executed without any screen or process feedback), logging all steps religiously is important. If something unexpected happens, you can always refer to the umbracoLog table to track down whether one of your event hooks caused the issue.

LISTING 12-17: AutoExpireFaq.cs

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

namespace UmbUsersGuide.Samples
{
    public class AutoExpireFaq : ApplicationBase
    {
        public AutoExpireFaq()
        {
            Document.BeforePublish += new
            Document.PublishEventHandler(Document_BeforePublish);
        }

        /// <summary>
        /// Event Handler that gets hit before an item is published.
        /// </summary>
        void Document_BeforePublish(Document sender,
                   umbraco.cms.businesslogic.PublishEventArgs e)
        {
            // log entry of event

umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Custom,
                    0,
                    “Document_BeforePublish: AutoExpireFaq STARTED”);

            // Check to make sure that the node that is being published
            // is of Document Type FAQ
            if (sender.ContentType.Alias == “FAQ”)
            {

umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Custom,
                    0,
                    “Document_BeforePublish: FAQ Doc Type matched”);

                // Check to make sure that that the expiration date
                // is not already set.
                // NOTE: ExpireDate corresponds to the Expiration
                // date field in the properties tab
                if (sender.ExpireDate != DateTime.MinValue &&
                  sender.ExpireDate < DateTime.Now)
                {
umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Custom,
                    0,
                    “Document_BeforePublish: CANCELLED - FAQ
                     expiration date already set”);
                    e.Cancel = true;
                }
                else
                {
umbraco.BusinessLogic.Log.Add(umbraco.BusinessLogic.LogTypes.Custom,
                    0,
                    “Document_BeforePublish: FAQ expiration date set”);

                    // Add 7 days to the current date and set that as
                    // the auto expiration date
                    sender.ExpireDate = DateTime.Now.AddDays(7);
                    sender.Save();
                }
            }
        }
    }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset