Get All Children of Page



This is my first attempt at a EpiServer site and I'm trying to figure out how to get the children of a particular page. I'm using EpiServer 7.5 with MVC. I'm creating an API controller that returns x number of blog posts with an offset of y. In EpiServer the currentPage is based on context. The Controller Action will determine exactly what the current page is. But what happens if you want to create that context by saying give me all children of the page tree node called "blog"? How do I get the node called "blog"? I've tried to use PageReference.ParseUrl("/blog") but this doesn't work. I can't use the GetPage function:

DataFactory.Instance.GetPage(new PageReference(10))

because the ID may be different in each environment. Is there a standard way of doing this in EpiServer?

Thank you.









Jul 16, 2014 19:11

Hi Akito, You could traverse up the tree until you find a page of pagetype (BlogHome)... by traverse I mean get parent in a recursive function. Another way would be to add a reference to blog from PostX when it is created, you can attach to the created event and check if the page type created is the one your looking for, then do the traverse and save the reference to blog in a property.
Any other suggestions community?

Jul 16, 2014 19:50

Don't know if there is a standard way of doing it but what I find myself doing and often see in other solutions is to but some properties as settings for the site on the startpage because episerver gives a static getter to the startpage reference. I also often find myself adding a startpage property to my baseclass for my pages.

So I would suggest something like this:

/// Base class for all page types
public abstract class BasePageData : PageData
	/// Returns the Start page
	public StartPage StartPage
			return DataFactory.Instance.Get(ContentReference.StartPage);

/// Page definition for Start page
public class StartPage : BasePageData
	/// Reference to News container page
	[Display(Order = 200, GroupName = SystemTabNames.Settings)]
	public virtual PageReference NewsContainerPage { get; set; }

var blogChildren = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance().GetChildren(StartPage.NewsContainerPage);
Jul 16, 2014 19:57

Thanks Jacob and Petter for your input.

We ended up adding a BlogListing PageReference (parent of all blog posts) to the StartPage/HomePage and then referencing that in the APIController:

var homePage = DataFactory.Instance.GetPage>(ContentReference.StartPage);
IEnumerablePage> blogPages = DataFactory.Instance.GetChildrenPage>(homePage.BlogListingReference);

Not sure if that would be best practice but it works. If I can find a better way to accomplisg this I will post it.

Thanks again!


Jul 16, 2014 22:50
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.