Tridion Event System 2011

November 29th, 2011 | Posted by Robert Curlette in Tridion - (10 Comments)

The Tridion 2011 upgrade brings a whole new set of possibilities for the Tridion Event System. Exciting improvements include Asynchronous events, multiple Event System DLL support, configuration via XML file, and a much more flexible architecture. Overall a great effort from Tridion and a much welcomed one. :)

Advantages to 2011 Event System

  • Native .Net architecture
  • Multiple Event systems per server
  • Asynchronous events
  • Uses delegates to subscribe to Events
  • Additional Event Phases (Abort, InDoubt, Processed)
  • Configured via XML file. Can easily see which .dlls will be called by the Event System

Event Phases

The Pre (Initiated) and Post (Processed) are still there, and are joined by 3 new Event Phases.

  • Initiated = Pre
  • Processed = Post
  • TransactionCommitted = Post
  • TransactionAborted
  • TransactionInDoubt

Sample Event System Project

Getting started with the new Tridion Event System in 2011 is easier and a complete different experience than with previous Event Systems. I will walk through setting up a new Event System for 2011 and implementing 1 sample event.

Let’s create a sample event system project in Tridion 2011 for the famous OnComponentSavePre event. For this demo I will use Visual Studio 2010, but you can also use VS 2008 or VS 2011. Following these examples it is easy to map an older Event System to then new Tridion 2011 model. We also gain a lot more flexibility, control, and predictability over the Events in the implementation.

Create a new Windows Class project from Visual Studio

– Target Framework can be .Net 4.0 or .Net 3.5

Add references and using statements

DLLs are located at \{install folder}\Tridion\bin\client
– Tridion.Common.dll
– Tridion.ContentManager
– Tridion.Logging
– Tridion.ContentManager.Publishing (if using Publish Events)

using System;
using Tridion.ContentManager.ContentManagement;
// Add Reference to 'Tridion.ContentManager', located @ Tridion\bin\client on CMS Server
using Tridion.ContentManager.Extensibility;
using Tridion.ContentManager.Extensibility.Events;
using Tridion.ContentManager.Publishing;
using Tridion.Logging;

Create the Class

Tridion calls all of our Event Systems configured in the Tridion.ContentManager.Config file. For this reason, we need to make sure the class attribute [TcmExtension("SomethingUnique")] is placed on our Event System class.

The class extends the TcmExtension class and this is needed on our class for Tridion to implement the methods.

namespace Tridion.EventSystem.v2011
{
    // The class attribute is unique per Event System dll on the server.
    // In 2011 we can have multiple Event System dlls on 1 server.
    [TcmExtension("Tridion2011EventSystemRC")]  // This needs to be unique per Event System
    public class EventSystem : TcmExtension
    {

Subscribe to Events

Place all of your subscribe calls in the class constructor or create a method as I do here called Subscribe and place the subscription methods there. Tridion only calls the methods in the Constructor when firing Events.

The .Subscribe method signatures are very special and needs all of the params to work and in this order. The online documentation describes the method signatures in more detail. See the documentation link at http://www.sdltridionworld.com.

public EventSystem()
{
	Subscribe();
}

public void Subscribe()
{
	// OnComponentSavePre
	EventSystem.Subscribe<Component, SaveEventArgs>(OnComponentSavePre, EventPhases.Initiated);
}

Component is the object we are subscribing to and SaveEventArgs is the Event we listen for. There are lots of EventArgs and it is best to use the Visual Studio object browser to see them all.

OnComponentSavePre is MY method and I could call it OnComponentDoThisBefore if I wanted to, but I use the original R5 names to keep it simple.

EventPhases.Initiated is the new -Pre and TransactionCommitted is the new -Post.

Implement Events

Create the methods to handle the event with the above method signature. The Params must match the signature of the Subscribe event. The order should be the same, (Object, Event, Phase).

private static void OnComponentSavePre(Component comp, SaveEventArgs args, EventPhases phase)
{
	comp.Title = "a " + comp.Title;
	args.ContextVariables.Add("StartTime", DateTime.Now);
}

Here I use Component for the Object and update the Title when saving. This is one way to test if the Event system is working. If I used a Post method I would need to throw an error to the Event Log or use a Logger to test if it works.

If I wanted to capture the Event for all items, I could subscribe to the IdentifyableObject and if I only wanted to catch the Events on VersionedITems (think Component, Page, Template, etc) then I could subscribe to a VersionedItem – both new in 2011.

Handling Errors

Any Pre- Errors show up in the new Tridion Message Center. This is really nice and much improved from previous error dialogs. The messages are saved and we can view them later. This implementation of errors is very handy when debugging the -Pre Events, now called ‘Initiated’.

The Tridion.Common assembly is needed for the TridionException event.

Installation / configuration

  • Copy the compiled DLL to the CMS Server
  • Open the Tridion.ContentManager.config file
  • Add the dll to the extensions:
    <add assemblyFileName=”C:\Program Files (x86)\Tridion\bin\Tridion.EventSystem.v2011.dll”/>
  • Shut down COM+
  • Restart the ‘Tridion Content Manager Service Host’ Service

Summary

I really enjoy working with the new Event System and think the Async support, multiple event systems, and .NET 4.0 integration will provide a stable foundation for the future. A big thanks to the Tridion R&D team for doing a great job with the new and improved Event System for Tridion 2011.

Full Code:

using System;
using Tridion.ContentManager.ContentManagement;
// Add Reference to 'Tridion.ContentManager', located @ Tridion\bin\client on CMS Server
using Tridion.ContentManager.Extensibility;
using Tridion.ContentManager.Extensibility.Events;

namespace Tridion.EventSystem.v2011
{
    [TcmExtension("Tridion2011EventSystemRC")]  // This needs to be unique per Event System
    public class EventSystem : TcmExtension
    {
        public EventSystem()
        {
            Subscribe();
        }

        public void Subscribe()
        {
            // OnComponentSavePre
            EventSystem.Subscribe<Component, SaveEventArgs>(OnComponentSavePre, EventPhases.Initiated);
        }

        private static void OnComponentSavePre(Component comp, SaveEventArgs args, EventPhases phase)
        {
            comp.Title = "a " + comp.Title;
            args.ContextVariables.Add("StartTime", DateTime.Now);
        }
    }
}

Tridion WebDav URL

November 9th, 2011 | Posted by Robert Curlette in Tridion - (0 Comments)

Tridion makes all items available using a WebDav URL. This is an environment independent variable that never changes. It is preferred to use this instead of an item URI when programming templates or scripts.

I recently needed a WebDavURL for a Folder and wrote the following snippet in a Component Template and then previewed with a Component in the desired folder:

[%
writeout component.organizationalitem.info.webdavurl
%]

This WebDavURL can be used anyplace you would normally use a URI.

The WebDavURL is also available on the info tab – however, on that screen you cannot copy it.