Search code examples
episerver

How to show computed property values in EPiServer 8?


In page edit mode I want to show a read-only text that is based on a page property value. The text could for example be "A content review reminder email will be sent 2015-10-10", where the date is based on the page published date + six months (a value that will be configurable and therefore can change anytime). So far I've tried to accomplish something like this by adding another property on the page.

I've added the property CurrentReviewReminderDate to an InformationPage class we use. In page edit mode the property name is shown, but it doesn't have a value. How do I do to show the value in page edit mode (preferably as a label)?

[CultureSpecific]
[Display(
    Name = "Review reminder date",
    Description = "On this date a reminder will be sent to the selected mail to remember to verify page content",
    Order = 110)]
[Editable(false)]
public virtual string CurrentReviewReminderDate
{
    get
    {
        var daysUntilFirstLevelReminder =
            int.Parse(WebConfigurationManager.AppSettings["PageReviewReminder_DaysUntilFirstLevelReminder"]);
        if (CheckPublishedStatus(PagePublishedStatus.Published))
        {
            return StartPublish.AddDays(daysUntilFirstLevelReminder).ToString();
        }
        return "";
    }
    set
    {
        this.SetPropertyValue(p => p.CurrentReviewReminderDate, value);
    }
}

Solution

  • Another solution would be to hook into the LoadedPage-event and add the value from there. This might not be the best way performance-wise since you need to do a CreateWritableClone, but depending on the site it might not matter.

        [InitializableModule]
    [ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
    public class EventInitialization : IInitializableModule
    {
        public void Initialize(InitializationEngine context)
        {
            ServiceLocator.Current.GetInstance<IContentEvents>().LoadedContent += eventRegistry_LoadedContent;
        }
    
        void eventRegistry_LoadedContent(object sender, ContentEventArgs e)
        {
            var p = e.Content as EventPage;
            if (p != null)
            {
                p = p.CreateWritableClone() as EventPage;
                p.EventDate = p.StartPublish.AddDays(10);
                e.Content = p;
            }
        }
    }