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.