November Happy Hour will be moved to Thursday December 5th.

How to alter a page's data On Save?

Vote:
 

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.  

#49382
Mar 15, 2011 23:47
Vote:
 

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?

#49393
Mar 16, 2011 8:58
Vote:
 

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);

#49401
Mar 16, 2011 10:22
Vote:
 

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/

#49415
Mar 16, 2011 13:11
Vote:
 

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.

 

#49428
Mar 16, 2011 20:41
Vote:
 

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

#49429
Mar 16, 2011 20:46
Vote:
 

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

#49430
Mar 16, 2011 21:37
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.