7

I have an EventSystem handler that sets a default metadata schema if a new page is created. This works perfect if I create the page in the CME. But if I create a new page (based on a Page Type) in XPM, the EventSystem doesn't kick in. So my metadata schema isn't set on the page. (Metadata schema has 1 required text-field, with a default value)

In my EventSystem handler I subscribe using the following code:

EventSystem.Subscribe<Page, LoadEventArgs>(SetDefaultSchema, EventPhases.Processed);

I also tried this (CrudEventArgs):

EventSystem.Subscribe<Page, CrudEventArgs>(SetDefaultSchema, EventPhases.Processed);

Both work like a charm in the CME, but not when the page is created in XPM. Any clues what I'm doing wrong?

(Tridion 2011, SP1 HR2)

1 Answer 1

8

My guess would be that when you are creating a new page based on page type in XPM, XPM is actually cloning/copy and paste the type and then updating it with the appropriate values. Recommend subscribing to CopyEventArgs to confirm.

Update

Creating a new page in XPM using a Page Type should fire CopyEventArgs, however, copy operation is done on system, and works on the persisted state of the item, not the transient state, so any property you change on the event args will not be copied to the destination. I would recommend that in the Processed phase of CopyEventArgs you will have access to CopiedObject which you can use to update.

EventSystem.Subscribe<Page, CopyEventArgs>(PageCopyEventHandler, EventPhases.Processed);

public void PageCopyEventHandler(Page source, CopyEventArgs e, EventPhases phase)
{
    if (phase == EventPhases.Processed && e.CopiedObject != null)
    {
        Page page = (Page) e.CopiedObject;
        bool isCheckedOutAlready = false;
        if (!page.IsCheckedOut)
        {
            isCheckedOutAlready = true;
            page.CheckOut();
        }
        page.MetadataSchema = SomeSchema;
        if (isCheckedOutAlready)
        {
            page.Save();
        }
        else
        {
            page.Save(true);
        }
    }
}
2
  • In which case you could probably set the metadata schema on the "original" page prototype and skip this in XPM?
    – Nuno Linhares
    Commented Dec 11, 2013 at 10:45
  • 1
    @Likhan: works like a charm. In my original handler I checked for page.Id.IsUriNull, but this was never the case when the page was created in XPM. It worked in the CME, but not in XPM. Your handler together with my original LoadEventArgs handler for CME actions is good enough for me. Thanks. Commented Dec 16, 2013 at 6:53

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.