Take the 2-minute tour ×
Tridion Stack Exchange is a question and answer site for Tridion developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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 Jan 20 at 12:04
add comment

2 Answers

up vote 12 down vote accepted

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.

share|improve this answer
    
Thanks, the EventPahase was indeed the wrong one. It tryed to add metadata after the component was saved. –  Roel Van Roozendaal Jan 20 at 14:07
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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