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.
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.
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.
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
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(); } } } } }