6

I'm trying to rewrite the old event system (2009) to the new 2013 event system. Where it used to be a OnFolderPasteItemPost event. I now register the event as follows:

EventSubscription _subscription;
_subscription = EventSystem.Subscribe<Folder, MoveEventArgs>(
                              HandlerForFolderPostCutPaste, EventPhases.Processed);
_subscription = EventSystem.Subscribe<Folder, CopyEventArgs>(
                              HandlerForFolderPostCopyPaste, EventPhases.Processed);

Which calls to the following code:

private void HandlerForFolderPostCutPaste(
             Folder subject, MoveEventArgs args, EventPhases phase)
{
    Trace.WriteLine("Called OnFolderPasteItemCommittedEvent on Events: " 
            + Utilities.GetPubEventSystem(subject.OwningRepository.Title
            , config).ToString(), "EventSystem");
}

private void HandlerForFolderPostCopyPaste(Folder subject, 
                   CopyEventArgs args, EventPhases phase)
{
    Trace.WriteLine("Called OnFolderPasteItemCommittedEvent on Events: " 
    + Utilities.GetPubEventSystem(subject.OwningRepository.Title, config).ToString(),  
    "EventSystem");
}

But when i place breakpoints on the methods, it will not break. Nor will it write anything to the trace.

I'm attaching to the process: TcmServiceHost.exe

6

Alright i found out how the event works and therefor solved the issue.

The MoveEvent & CopyEvent aren't organizationalItem events as it used to be. It are events on items. This is not really clear from this matrix though.

So although technically my code isn't wrong: The call <Folder, MoveEventArgs/CopyEventArgs> will only fire when a Folder is copied/moved into another Folder. What i was looking for was <Component, MoveEventArgs/CopyEventArgs>. I tried this and my breakpoint got hit and a log entry was made.

5

Here is the code of the subscriptions. As said above, all other events are fired. Just not the move and copy ones.

using System;
using System.Configuration;
using Justitie.Tcm.EventHandlers.EventTypes;
using Justitie.Tcm.EventHandlers.HelperClasses;
using Tridion.ContentManager.CommunicationManagement;
using Tridion.ContentManager.ContentManagement;
using Tridion.ContentManager.Extensibility;
using Tridion.ContentManager.Extensibility.Events;

namespace Justitie.Tcm.EventHandlers
{
[TcmExtension("EventsHandler")]
public class EventsHandler : TcmExtension, IDisposable
{
    EventSubscription _subscription;
    private JustitieEvents eventSystem = null;
    private string exeConfigPath;
    private Configuration config;

    public EventsHandler()
    {
        exeConfigPath = this.GetType().Assembly.Location;
        try
        {
            config = ConfigurationManager.OpenExeConfiguration(exeConfigPath);
        }
        catch (Exception ex)
        {
            Logger.WriteLog(LogLevelL4N.ERROR, "No Config file could be found. Errmsg: " + ex.Message);
        }

        Subscribe();
    }

    public void Subscribe()
    {
        //Component save events
        _subscription = EventSystem.Subscribe<Component, SaveEventArgs>(HandlerForComponentPreSave, EventPhases.Initiated);
        _subscription = EventSystem.Subscribe<Component, SaveEventArgs>(HandlerForComponentPostSave, EventPhases.Processed);

        //Component Delete events
        _subscription = EventSystem.Subscribe<Component, DeleteEventArgs>(HandlerForComponentPreDelete, EventPhases.Initiated);
        _subscription = EventSystem.Subscribe<Component, DeleteEventArgs>(HandlerForComponentPostDelete, EventPhases.Processed);

        //Component Publish Events
        _subscription = EventSystem.Subscribe<Component, PublishEventArgs>(HandlerForComponentPrePublish, EventPhases.Initiated);

        //Page save events
        _subscription = EventSystem.Subscribe<Page, SaveEventArgs>(HandlerForPagePreSave, EventPhases.Initiated);

        //Page Publish Events
        /*_subscription = EventSystem.Subscribe<Page, SetPublishStateEventArgs>(HandlerForPageSetToPublish, EventPhases.Processed);
        _subscription = EventSystem.Subscribe<Page, PublishEventArgs>(HandlerForPagePrePublish, EventPhases.Initiated);
        _subscription = EventSystem.Subscribe<Page, PublishEventArgs>(HandlerForPagePostPublish, EventPhases.Processed);
        _subscription = EventSystem.Subscribe<Page, UnPublishEventArgs>(HandlerForPagePostUnPublish, EventPhases.Processed);*/

        //Folder PasteItem Events
        _subscription = EventSystem.Subscribe<Folder, MoveEventArgs>(HandlerForFolderPostCutPaste, EventPhases.Processed);
        _subscription = EventSystem.Subscribe<Folder, CopyEventArgs>(HandlerForFolderPostCopyPaste, EventPhases.Processed);

    }

    private void HandlerForComponentPreSave(Component subject, SaveEventArgs args, EventPhases phase)
    {
        Logger.WriteLog(LogLevelL4N.DEBUG, "Called OnComponentSaveInitiatedEvent on Events: " + Utilities.GetPubEventSystem(subject.OwningRepository.Title, config));
        Utilities.LoadEventSystem(exeConfigPath, Utilities.GetPubEventSystem(subject.OwningRepository.Title, config), subject.Session, ref eventSystem);
        eventSystem.ComponentSavePre(subject, args);
    }

    private void HandlerForComponentPostSave(Component subject, SaveEventArgs args, EventPhases phase)
    {
        Logger.WriteLog(LogLevelL4N.DEBUG, "Called OnComponentSaveCommittedEvent on Events: " + Utilities.GetPubEventSystem(subject.OwningRepository.Title, config));
        Utilities.LoadEventSystem(exeConfigPath, Utilities.GetPubEventSystem(subject.OwningRepository.Title, config), subject.Session, ref eventSystem);
        eventSystem.ComponentSavePost(subject, args);
    }

    private void HandlerForComponentPreDelete(Component subject, DeleteEventArgs args, EventPhases phase)
    {
        Logger.WriteLog(LogLevelL4N.DEBUG, "Called OnComponentDeleteInitiatedEvent on Events: " + Utilities.GetPubEventSystem(subject.OwningRepository.Title, config));
        Utilities.LoadEventSystem(exeConfigPath, Utilities.GetPubEventSystem(subject.OwningRepository.Title, config), subject.Session, ref eventSystem);
        eventSystem.ComponentPreDelete(subject);
    }

    private void HandlerForComponentPostDelete(Component subject, DeleteEventArgs args, EventPhases phase)
    {
        Logger.WriteLog(LogLevelL4N.DEBUG, "Called OnComponentDeleteCommittedEvent on Events: " + Utilities.GetPubEventSystem(subject.OwningRepository.Title, config));
        Utilities.LoadEventSystem(exeConfigPath, Utilities.GetPubEventSystem(subject.OwningRepository.Title, config), subject.Session, ref eventSystem);
        eventSystem.ComponentPostDelete(subject);
    }

    private void HandlerForComponentPrePublish(Component subject, PublishEventArgs args, EventPhases phase)
    {
        Logger.WriteLog(LogLevelL4N.DEBUG, "Called OnComponentPublishInitiatedEvent on Events: " + Utilities.GetPubEventSystem(subject.OwningRepository.Title, config));
        Utilities.LoadEventSystem(exeConfigPath, Utilities.GetPubEventSystem(subject.OwningRepository.Title, config), subject.Session, ref eventSystem);
        eventSystem.ComponentPrePublish(subject, args);
    }

    private void HandlerForPagePreSave(Page subject, SaveEventArgs args, EventPhases phase)
    {
        Logger.WriteLog(LogLevelL4N.DEBUG, "Called PageSavePre " + phase.ToString() + " on Events: " + Utilities.GetPubEventSystem(subject.OwningRepository.Title, config).ToString());
        Utilities.LoadEventSystem(exeConfigPath, Utilities.GetPubEventSystem(subject.OwningRepository.Title, config).ToString(), subject.Session, ref eventSystem);
        eventSystem.PageSavePre(subject, args);
    }


    private void HandlerForPageSetToPublish(Page subject, SetPublishStateEventArgs args, EventPhases phase)
    {
        Logger.WriteLog(LogLevelL4N.DEBUG, "Called PageSetToPublish " + phase.ToString() + " on Events: " + Utilities.GetPubEventSystem(subject.OwningRepository.Title, config).ToString());
        Utilities.LoadEventSystem(exeConfigPath, Utilities.GetPubEventSystem(subject.OwningRepository.Title, config).ToString(), subject.Session, ref eventSystem);
        eventSystem.PageSetPublishedToPost(subject, args);
    }

    private void HandlerForPagePrePublish(Page subject, PublishEventArgs args, EventPhases phase)
    {
        Logger.WriteLog(LogLevelL4N.DEBUG, "Called PagePrePublish " + phase.ToString() + " on Events: " + Utilities.GetPubEventSystem(subject.OwningRepository.Title, config).ToString());
        Utilities.LoadEventSystem(exeConfigPath, Utilities.GetPubEventSystem(subject.OwningRepository.Title, config).ToString(), subject.Session, ref eventSystem);
        eventSystem.PagePrePublish(subject, args);
    }

    private void HandlerForPagePostPublish(Page subject, PublishEventArgs args, EventPhases phase)
    {
        Logger.WriteLog(LogLevelL4N.DEBUG, "Called PagePostPublish " + phase.ToString() + " on Events: " + Utilities.GetPubEventSystem(subject.OwningRepository.Title, config).ToString());
        Utilities.LoadEventSystem(exeConfigPath, Utilities.GetPubEventSystem(subject.OwningRepository.Title, config).ToString(), subject.Session, ref eventSystem);
        eventSystem.PagePostPublish(subject, args);
    }

    private void HandlerForPagePostUnPublish(Page subject, UnPublishEventArgs args, EventPhases phase)
    {
        Logger.WriteLog(LogLevelL4N.DEBUG, "Called PagePostUnPublish " + phase.ToString() + " on Events: " + Utilities.GetPubEventSystem(subject.OwningRepository.Title, config).ToString());
        Utilities.LoadEventSystem(exeConfigPath, Utilities.GetPubEventSystem(subject.OwningRepository.Title, config).ToString(), subject.Session, ref eventSystem);
        eventSystem.PagePostUnPublish(subject, args);
    }

    private void HandlerForFolderPostCutPaste(Folder subject, MoveEventArgs args, EventPhases phase)
    {
        Logger.WriteLog(LogLevelL4N.DEBUG, "Called OnFolderPasteItemCommittedEvent on Events: " + Utilities.GetPubEventSystem(subject.OwningRepository.Title, config).ToString());
        Utilities.LoadEventSystem(exeConfigPath, Utilities.GetPubEventSystem(subject.OwningRepository.Title, config).ToString(), subject.Session, ref eventSystem);
        eventSystem.FolderPostCutPasteItem(subject, args);
    }

    private void HandlerForFolderPostCopyPaste(Folder subject, CopyEventArgs args, EventPhases phase)
    {
        Logger.WriteLog(LogLevelL4N.DEBUG, "Called OnFolderPasteItemCommittedEvent on Events: " + Utilities.GetPubEventSystem(subject.OwningRepository.Title, config).ToString());
        Utilities.LoadEventSystem(exeConfigPath, Utilities.GetPubEventSystem(subject.OwningRepository.Title, config).ToString(), subject.Session, ref eventSystem);
        eventSystem.FolderPostCopyPasteItem(subject, args);
    }

    public void Dispose()
    {
        _subscription.Unsubscribe();
    }
}
}
  • 1
    Haven't spotted your problem yet, but take note that you are overwriting _subscription each time, so your Dispose() method will only unhook the last subscription. – Dominic Cronin Jun 11 '13 at 21:34
  • But it will dispose all then or am i wrong? – Allard Buijen Jun 13 '13 at 10:22
  • If your dispose doesn't unhook them all, then Tridion will keep hold of your event handler via the events system's delegate chain. You'll probably leak memory all over the floor. Wear your wellies! – Dominic Cronin Jun 13 '13 at 12:22
  • You're quite wright. I now load all the events in a subscription list. And in my Dispose i have: foreach (EventSubscription subscription in _subscriptions) { subscription.Unsubscribe(); } – Allard Buijen Jun 13 '13 at 13:05
3

It sounds like your event system is not registered at all. Are other events working OK? Check in the event log to see if your event system is registered at all - you should see a line in the Tridion event log saying your event dll has been loaded.

One cause I have seen is that the event system class is not public (see What might cause my events system not to "fire" when events take place?). Can you post the full code for your event class (or at least the class definition and contructor?)

  • All events are fired just fine. I'll post the code in a new answer. – Allard Buijen Jun 11 '13 at 10:41
  • That weird then... I cant see anything wrong by scanning through the code - maybe its a bug? – Will Price Jun 11 '13 at 11:00
  • you mean a feature :) – Allard Buijen Jun 11 '13 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.