How to alter a page's data On Save?

I have a "Meeting" pagetype extending EPiServer.TemplatePage.  A property defined for this pagetype is "MeetingDate" - set to a required value.

When an editor creates a Meeting I want to set the StopPublish property to be 6 months after whatever the editor set as the MeetingDate.

My initial expectation was that I could override the SavePage along the following lines: 

public override bool SavePage()
{
// set the stopPublish date to be MeetingDate + 6months
return base.SavePage ();
}

public override bool SavePage()

  {

  // set the stopPublish date to be MeetingDate + 6months

... the code for doing that


return base.SavePage ();

}

But this method is not getting called when Save and Publish is invoked by the editor. 

I've read myself silly on this now and would appreciate clues on how to progress.  I have a feeling that I must latch on to EditPage class somehow.  


  • I should mention that this is possible using Page Type settings in admin mode:
    Edit Page type --> On the Default values tab, check the "Use adjusted default settings..." and then set Stop Publish related to page creation date.

    However, If you want to do this by code, I need to know what version of EPiServer this is related to?

  • EPiServer 5 (haven't tried in 6):

    Hook up to the DataFactory.Instance.PublishingPage event in Application_Start in Global.asax.cs. 
    ....
    DataFactory.Instance.PublishingPage += new PageEventHandler(Your_Method_Name);
    ....

     

    In the method you create, you can do something like:

    PageData pd = e.Page.CreateWritableClone();
    pd.StopPublish = pd.StartPublish.AddMonths(6);
    DataFactory.Instance.Save(pd, SaveAction.Publish);

  • It is best if you attach your event handler using an InitializableModule, see http://tedgustaf.com/sv/blogg/2010/5/attach-episerver-event-handlers-on-startup-using-initializablemodule/

  • Thanks all.

    For others referencing this thread, sorry to have ommitted the details: EPiServer CMS6

    @Mari: thanks for that tip which I didn't know about.  I will investigate if it is possible to do it with my custom field for "MeetingDate" as it is not generally related to the creation date.

     

  • Take Ted's code example in the link from Magnus' post, and put my code in the CreatedPageHandler method.

  • In the publishing event you should not save the page, but just alter the property

    e.Page.StopPublish= e.Page.StartPublish.AddMonths(6);

    The saving of the page will be done by episerver

First   1   Last