Tridion Event System 2011

November 29th, 2011 | Posted by Robert Curlette in Tridion

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

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

10 Responses

  • Rick Pannekoek says:

    Note that the old-style “Post” event maps to the “Processed” phase rather than the “TransactionCommitted” phase: the event gets triggered after the work has been done, but *before* the transaction commits.
    That implies that a “Post”/”Processed” event handler can still let the transaction fail/rollback, whereas a “TransactionCommitted” event handler cannot.
    The difference between the two becomes even more apparent when multiple CM operations are combined in a single transaction.

  • Great post Robert. Unfortunately the Subscribe call seems to be missing the type designation (most likely it was filterer out by your blog software):

    So:
    EventSystem.Subscribe(OnComponentSavePre, EventPhases.Initiated);
    Should be:
    EventSystem.Subscribe<Component, SaveEventArgs>(OnComponentSavePre, EventPhases.Initiated);

    Fingers crossed that the angular brackets make it through in that last line.

  • robert curlette says:

    @Rik – Thanks for the clarification! I have updated the post above. So, if my Post event is business critical I should always use the Processed phase and the error it will be thrown inside the GUI message system? And if I use the TransactionCommit then it will fail silently and log to the Event log?

    @Puf – Thanks a lot for the find. You were right – wordpress was removing the angle brackets – and that was a critical piece of code! The code is updated above.

  • I am having issues with the event system. When ever I install the Extension, I receive an error. It appears to be a setup issue or a permissions issue. Any idea?

    Web Page cannot load
    Source TridionMessages already exists on the local computer.
    Unable to get LangID of User (tcm:0-13-65552). Initialization of the extension ‘C:\Temp\EventSystemTest5\Healthgrades.Tridion.EventSystem.dll’ failed.

  • robert curlette says:

    2011 SP1 updates how the messaging system works. Is it possible you upgraded to SP1 but the Event System uses 2011 GA DLLs?

  • Alvin says:

    Thanks Robert. Some extension examples don’t mention the specific dlls. I appreciate being able to find your example via Google quicker than actually reading the manual like I should. 😉

  • where’s the “Tridion Message Center” located? Are you referring to the Windows Event Viewer and the Tridion and Tridion Content Manager event log? There’s nothing I can see in the Tridion Content Manager MMC console.

    The Content Manager Explorer web UI has a “Event Log” in the Administration ribbon, which I’m assuming retrieves records from the database TRIDION_CM_LOG.

  • robert curlette says:

    The Message Center is the term for the popup notifications such as ‘Your pages have been sent to the Publish Queue.’ There is also a flag in the top left that opens the Message Center to show old messages.

    Hope this helps.

  • Premkumar says:

    @Rick,

    You have mentioned Note that the old-style “Post” event maps to the “Processed” phase rather than the “TransactionCommitted” phase: the event gets triggered after the work has been done, but *before* the transaction commits.

    But the old-style “Post” event takes place only after successful content manager action (i.e., after transaction is committed).

    For example, automating the page creation after sucessful creation of Component.. This was earlier handled in ‘OnComponentSavePost’ event. Shouldn’t this be now handled during ‘TransactionCommitted’ phase?

  • D says:

    Can you please tell us the link for learning Event System in SDL Tridion ?



Leave a Reply

Your email address will not be published. Required fields are marked *