I am working on triggering an event to create a page automatically, when user creates a page.
namespace TestEventHandler
{
[TcmExtension("MyEventHandlerExtension")]
public class MyEventHandler : TcmExtension
{
public MyEventHandler()
{
Subscribe();
}
public void Subscribe()
{
EventSystem.Subscribe<Page, SaveEventArgs>(HandlerForProcessed, EventPhases.Processed);
//EventSystem.Subscribe<Page, PublishEventArgs>(HandlerForInitiated, EventPhases.Initiated);
}
private void HandlerForProcessed(Page subject, SaveEventArgs args, EventPhases phase)
{
//create page.
Tridion.ContentManager.Session mySession = new Tridion.ContentManager.Session(@"user");
XmlElement XmlElement = null;
Page newPage = new Page(XmlElement, mySession);
newPage.Title = subject.Title + "_auto";
newPage.FileName = subject.FileName + "_auto";
newPage.PageTemplate = subject.PageTemplate;
newPage.Save(true);
newPage.CheckOut();
}
}
}
It's not creating the page automatically. I think some modification is required for this code.
I am getting error "Impersonation by this user requires the Machinename\MTSUser account to be configured as impersonation user".
Can anyone help with the modifications needed? I am using the TOM.net API for this.
Thank you.
The error is due to the new Session you are trying to create. That should not be necessary. You can get it from Page subject.Session
.
Additionally, you are using the wrong constructor for the Page. Check out the documentation.
Sample code:
Page page = page = new Page(session, new TcmUri(sg));
page.Title = theTitle;
page.FileName = new Regex("\\W").Replace(theTitle, "");
page.PageTemplate = session.GetObject(pt) as PageTemplate;
ComponentTemplate componentTemplate = session.GetObject(ct) as ComponentTemplate;
page.ComponentPresentations.Add(
new ComponentPresentation(component, componentTemplate));
page.Save(true);
The parameters you need are sg
, theTitle
, pt
, ct
, component
. You could read them from Folder metadata for example.