Anders Hattestad
Aug 6, 2013
  8503
(0 votes)

Using Page Properties as dynamic properties in EPiServer 7

Dynamic properties was great for having properties that is inherit. But the new interface of EPiServer 7 makes this a bit difficult. One solution is to modify the EPiServer: Property code so it allows for finding the property that have value up in the page structure. And if the Property exist on the page allow the editor to add values to the current property and break the inherit chain.

The code is simple. Find the first property (can be a page block) that have value and display that

Code Snippet
  1. public class PropertyInherit : EPiServer.Web.WebControls.Property
  2. {
  3.     protected override void CreateChildControls()
  4.     {
  5.         try
  6.         {
  7.             InnerProperty = FindInheritBlock(PageSource.CurrentPage, PropertyName);
  8.             if (!InnerProperty.IsNull && PageSource.CurrentPage.Property[PropertyName] != null && PageSource.CurrentPage.Property[PropertyName].IsNull)
  9.             {
  10.                 //Is using inherit isUsingInherit = true;
  11.             }
  12.             if (PageSource.CurrentPage.Property[PropertyName] != null)
  13.             {
  14.                 this.Editable = true; //Property exist on current page, make it editable
  15.             }
  16.             base.CreateChildControls();
  17.  
  18.         }
  19.         catch
  20.         {
  21.             AddLiteral("Error in PropertyInherit");
  22.         }
  23.  
  24.     }
  25.     protected PropertyData FindInheritBlock(PageData page, string key)
  26.     {
  27.         var prop = page.Property[key];
  28.         if (prop != null && !prop.IsNull)
  29.         {
  30.             return prop;
  31.         }
  32.         if (PageReference.RootPage.CompareToIgnoreWorkID(page.PageLink))
  33.             return null;
  34.         return FindInheritBlock(EPiServer.DataFactory.Instance.GetPage(page.ParentLink), key);
  35.     }
  36.       
  37.     Literal AddLiteral(string key)
  38.     {
  39.         var lit = new Literal() { ID = key };
  40.         this.Controls.Add(lit);
  41.         return lit;
  42.     }
  43. }

If you have page block SideBarImage with no data, but the parent page have data it will display

image 

but when you try to edit it it will be empty

image

if you go to the parent page and try to edit it it will have data of course Smilefjes

 

image

You will need to register you new web control

Code Snippet
  1. <httpRuntime requestValidationMode="2.0" />
  2. <pages validateRequest="false" enableEventValidation="true" pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
  3.   <controls>
  4.     <add tagPrefix="Itera" namespace="Itera.WebControls" assembly="Itera" />

And you can use it like this

Code Snippet
  1. <Itera:PropertyInherit runat="server" PropertyName="SidebarImage" id="SidebarImage1" />
Aug 06, 2013

Comments

Henrik Fransas
Henrik Fransas Dec 11, 2013 02:44 PM

Any thoughts on how to do this in a MVC enviroment?

Jan 23, 2014 10:10 AM

I am also curious about the MVC way although it is possible to use "classic" usercontrols in MVC. I see that the Datafactory is used, shouldn't the Servicelocator be used instead? I have the idea the datafactory will be fased-out at some point.

Unknown
Unknown Nov 21, 2014 03:59 AM

If you have a model such as SitePageData in the Alloy example site from which all other page models inherit then you can do something like this
(Works for me)

[Display(
GroupName = SystemTabNames.Settings,
Order = 305)]
[CultureSpecific]
public virtual string SiteSection
{
get
{
return GetInheritedSection(this);
}
set { this.SetPropertyValue(p => p.SiteSection, value); }

}
public string GetInheritedSection(SitePageData page)
{
string result = page.GetPropertyValue(p => p.SiteSection);
if (string.IsNullOrWhiteSpace(result))
{
if (page.ParentLink != null)
{
SitePageData parent = EPiServer.DataFactory.Instance.GetPage(page.ParentLink) as SitePageData;
if (parent != null)
{
result = GetInheritedSection(parent);
}
}
}
return result;
}

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024