6

I'm setting up some basic functions for a Tridion 2013 SP1 EventSystem. The idea is simple, set the date on the metadata of a component when the metadata field is left empty. Now it seems that the event system thinks it's a new component and returns the following error.

An exception of type 'Tridion.ContentManager.ItemDoesNotExistException' occurred in Tridion.ContentManager.dll but was not handled in user code Additional information: The item tcm:3-4437-16-v0 does not exist.

The component uri of the component updating is: tcm:3-4437-16 so for some reason the eventsystem thinks it's a new component (-v0).

so here is my code:

EventSubscription

EventSystem.Subscribe<Component, SaveEventArgs>(ContentEvents.OnComponentSave, EventPhases.TransactionCommitted);

The Class Function

public static void OnComponentSave(Component component, SaveEventArgs args, EventPhases phase)
    {
        Log.Info(component, "OnComponentSave " + component.MetadataSchema.Title);

        if (component.Metadata!=null)
        {
            var metadata = new ItemFields(component.Metadata, component.MetadataSchema);
            var standardMeta = metadata.GetEmbeddedField("embed_standard_meta");

            Log.Info(component, "OnComponentSave There is a metadata schema ");

            if (standardMeta != null)
            {
                Log.Info(component, "OnComponentSave There is metadata");

                if (standardMeta.GetDateValue("date_creation") == null || ((DateField)standardMeta["date_creation"]).Value == DateTime.MinValue)
                {
                    ((DateField)standardMeta["date_creation"]).Value = DateTime.Now;
                    component.Metadata = metadata.ToXml();
                    Log.Info(component, "OnComponentSave " + DateTime.Now);
                }
            }
        }
    }

Hope any one can point me in the right direction, it's kinda frustrating.

1
  • 1
    user978511 has it right - you want this to happen before the item is saved. Just wanted to point out that -v0 means "current latest version", not "new item".
    – Nuno Linhares
    Commented Jan 20, 2014 at 12:04

2 Answers 2

14

The only obvious problem is that you should subscribe to EventPhases.Initiated instead of EventPhases.TransactionCommitted. Other than that it looks fine, unless you are calling component.Save somewhere down the line.

1
  • Thanks, the EventPahase was indeed the wrong one. It tryed to add metadata after the component was saved. Commented Jan 20, 2014 at 14:07
4

You are calling the event in transaction committed phase. But editable version of the component is not available during this phase. Subscribe to the event during Initiated phase.

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.