4

Is there a way to send object or even string or bool values from one event to another event in event system of Tridion 2011? Below is my scenario.

I have 2 events OnCompSave in Initiated phase and another event in OnCompSave in Committed phase. I want to pass some info from initiated phase event to committed phase event for avoiding validating the component's schema name twice as i am fetching the schema name to be compared from a config component.

Hope my use case isn't confusing.

1
  • 1
    Based on the comments below is it possible to add more information to the context of the question. What are you trying to pass between the events. Which events are you concerned with? Perhaps an outline of the use case(s) would assist in getting you a more specific answer... Oct 26, 2013 at 23:47

1 Answer 1

7

If you are using the Event System Handlers introduced in Tridion 2011, then you can easily pass information of any type between event handlers by using ContextVariables, which are loaded in the EventArgs of the event currently being triggered. The example below shows how to do that:

[TcmExtension("ComponentSaveEventHandler")]
public class ComponentSaveEventHandler
{
    public ComponentSaveEventHandler()
    {
        Subscribe();
    }
    public void Subscribe()
    {
        EventSystem.Subscribe<Component, PublishEventArgs>
            (ComponentPublishPreEventHandlerForInitiated, EventPhases.Initiated);
        EventSystem.Subscribe<Component, SaveEventArgs>
            (ComponentSaveHandlerForInitiated, EventPhases.Initiated);
    }

    private void ComponentPublishPreEventHandlerForInitiated
        (Component component, PublishEventArgs args, EventPhases phase)
    {
        // We need to do some action here, like auto editing metadata 
        // of a component about to be published
        if (AutoEditComponentMeta(component))
        {
            // Setting a string value here, but it can be any object.
            args.ContextVariables.Add("MetaIsAutoEdited", "Yes");
        }
    }

    private void ComponentSaveHandlerForInitiated
        (Component component, SaveEventArgs args, EventPhases phase)
    {
        // We can check the EventStack to see
        // whether this event was triggered by another event.
        // This is just an example.
        foreach (EventStackItem item in args.EventStack)
        {
            if (item.EventArgs is PublishEventArgs)
            {
                if (item.EventArgs.ContextVariables.ContainsKey("MetaIsAutoEdited"))
                {
                    Log.Info("We have already processed this item. Breaking off.");
                    return;
                }
            }
        }
    }

    private bool AutoEditComponentMeta(Component component)
    {
        // pseudo code
        // do some editing and save the component, 
        // which will also trigger the Save event handler.
        component.Save();
        return true;
    }
}

Further information can be found in the online documentation (requires login). Especially the order in which event phases occur for different events in the event hierarchy is worth taking a look at.

3
  • Nice explanation Raimond! Oct 26, 2013 at 11:59
  • Note, you cannot pass ContextVariables between events handled by the CM service, e.g. Save events, and Publisher service, e.g. Publish events. Oct 26, 2013 at 13:58
  • True, but the publishpre eventargs do work.:)
    – Raimond
    Oct 26, 2013 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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