I am creating an eventsystem function that is triggered when a new Page is created. The function should, when a new Page is created, set the metadata Schema of that Page, and create a Component that is then inserted in the Page metadata Component link field.
This function works (see sample code below), however, I have noticed that this function is triggered twice (as two Components are being created) Also, when I debug this function, it hits the breakpoint twice.
To confirm that nothing inside the code below could trigger a second load event, I commented out all code after the first if, but still this is being triggered twice.
I also tried to change the event phase to initiated. This is only triggered once, but it seems I can't change anything to the Page at that point (any changes I do are ignored).
I'm not sure if this is because I'm doing something wrong, or if this is by design, or this is a bug. Anyone has any suggestions ?
Event system code :
public ContentCreation()
{
EventSystem.Subscribe<Page, LoadEventArgs>(OnPageCreate, EventPhases.Processed);
}
public void OnPageCreate(Page page, LoadEventArgs args, EventPhases phases)
{
if (page.Id == TcmUri.UriNull)//we have a new page, so setup the schema
{
Schema meta = page.Session.GetObject([WEBDAV_PATH]) as Schema;
page.MetadataSchema = meta;
page.Metadata = meta.GetInstanceData(page.OrganizationalItem, LoadFlags.Expanded).Metadata;
//now, create a new SEO component
string SeoSchemaUri = [WEBDAV_PATH];
string SeoFolderUri = [WEBDAV_PATH];
if (page.Session.IsExistingObject(SeoFolderUri) && page.Session.IsExistingObject(SeoSchemaUri))
{
Folder folder = page.Session.GetObject(SeoFolderUri) as Folder;
Schema seoschema = page.Session.GetObject(SeoSchemaUri) as Schema;
Component comp = folder.GetNewObject<Component>();
comp.Title = string.Format("Auto_Component_page_{0}", Guid.NewGuid().ToString("N"));
comp.Schema = seoschema;
comp.Content = seoschema.GetInstanceData(folder, LoadFlags.Expanded).Content;
comp.Save(true);
ItemFields pageMeta = new ItemFields(page.Metadata, page.MetadataSchema);
ComponentLinkField field = pageMeta["relatedContent"] as ComponentLinkField;
field.Value = comp;
page.Metadata = pageMeta.ToXml();
}
}
}
Update
As requested an extract of my Tridion.ContentManager.config
:
<extensions>
<add assemblyFileName="C:\Program Files (x86)\Tridion\bin\Tridion.AudienceManagement.ContentManagerEvents.dll" />
<add assemblyFileName="C:\Program Files (x86)\Tridion\bin\Tridion.UGC.EventSystem.dll" />
<add assemblyFileName="C:\Program Files (x86)\Tridion\bin\Extensions\EventSystem.dll" />
</extensions>
The C:\Program Files (x86)\Tridion\bin\Extensions\EventSystem.dll
is my eventsystem dll, the other ones are standard Tridion ones.
Update 2
The reason why I subscribe to the Page
, LoadEventArgs
is because we want to set some default data when a Page is created, so the editor can modify it if needed. We don't want to do this on save (this would mean the editor would have to create a Page, save it, reopen it and then change the data).