Skip to content

Instantly share code, notes, and snippets.

@kimgunnarsson
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kimgunnarsson/899b2e72ff5fe8619fa2 to your computer and use it in GitHub Desktop.
Save kimgunnarsson/899b2e72ff5fe8619fa2 to your computer and use it in GitHub Desktop.
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