Repository navigation
Children property example
NOTE: example here just shows how to add your own property. The code below is taken from the library code, so it's already implemented.
As an EPiServer site developer I want to have IEnumerable<MyBasePage> Children property on all of my content pages
so I can use it in my logic and easily set from unit tests.
##1. Define annotation attribute
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class CmsChildrenAttribute : Attribute
{}
##2. Define property getter
public class ChildrenPropertyGetter : IEPiPropertyGetter
{
private readonly IContentLoader _contentLoader;
protected virtual IContentLoader ContentLoader { get { return _contentLoader; } }
public ChildrenPropertyGetter(IContentLoader contentLoader)
{
// simple constructor injection
_contentLoader = contentLoader;
}
public virtual bool CanIntercept(IContentData contentData, PropertyInfo property)
{
// get property type
var propertyType = property.PropertyType;
// extract collection item type from it (i.e. for property type IEnumerable<ArticlePage> we'll extract ArticlePage type)
var itemType = propertyType.TryGetCollectionItemType();
// we handle this property if it's a collection with item type derived from PageData
return contentData is IContent && itemType != null && itemType.Is<PageData>();
}
public virtual object GetValue(IContentData contentData, PropertyInfo property)
{
var content = (IContent) contentData;
// get the collection item type.
var itemType = property.PropertyType.TryGetCollectionItemType();
if (itemType == null)
{
throw new InvalidOperationException("Unsupported property type " + property.PropertyType.FullName);
}
// create a new List<ItemType> to represent the results.
var resultType = typeof(List<>).MakeGenericType(itemType);
var result = (IList) Activator.CreateInstance(resultType);
// get the current page children as PageData
var children = ContentLoader.GetChildren<PageData>(content.ContentLink);
// and filter them by our ItemType (same as children.OfType<ItemType>())
foreach (var child in children.Select(x => x.Cast(itemType)).Where(child => child != null))
{
result.Add(child);
}
return result;
}
}
##3. Register our property getter [InitializableModule] public class EPiNavigationPropertiesInitializationModule : EPiPropertiesInitializationModuleBase { public override void ConfigureEPiProperties(EPiPropertiesRegistry registry) { registry.For().Use(); } }
##4. Add base page class with Children property defined Let's create a public property with internal setter, so we'll be able to easily set the value in unit tests
public class PageBase : PageData
{
[CmsChildren]
public virtual IEnumerable<PageBase> Children { get; internal set; }
}
##5. Create a test page ###Page type [ContentType(GUID = "{63302A4D-7577-4258-A519-38567F1EE3E0}")] public class FrontPage : Base.PageBase { public virtual string Greeting { get; set; } }
###Controller public class FrontController : PageController { public ActionResult Index(FrontPage currentPage) { return View(currentPage); } }
@model SampleSite.Models.Pages.FrontPage
<nav>
<ul>
@foreach (var page in Model.Children)
{
<li>
@Html.PageLink(page)
</li>
}
</ul>
</nav>