Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Code for using page properties as Dynamic Properties in EPiServer and retrieve their values up the chain. Code is provided as is. If you use it, you might consider doing some nice null- and empty checks.
using EPiServer;
using EPiServer.Core;
using EPiServer.ServiceLocation;
namespace Core.Properties
{
public static class PropertyFinder
{
public static T FindProperty<T>(PageData currentPage, string propertyName)
{
if (currentPage[propertyName] != null) {
return (T)currentPage[propertyName];
}
var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
var parentPage = contentRepository.Get<PageData>(currentPage.ParentLink);
if (parentPage == null) {
return default(T);
}
if (parentPage[propertyName] != null) {
return (T)parentPage[propertyName];
}
return FindProperty<T>(parentPage, propertyName);
}
}
}
private MyPropertyType _myInheritedProperty;
public MyPropertyType MyInheritedProperty
{
get
{
if (_myInheritedProperty == null)
{
_myInheritedProperty = Core.Properties.PropertyFinder.FindProperty<MyPropertyType>(CurrentPage, "MyInheritedProperty");
}
return _myInheritedProperty;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment