EPiServer : How To Get Unpublished Child Pages With Meta Data

I’ve recently taken over a project that was developed by a third party company.  The site uses a number of hub/stack pages to pull in child pages to make up the page content to automatically create page placeholders from a side nav.  We had an issue where content editors could not fully preview a stack page correctly if some of the sub pages hadn’t been published. I had to change the template to allow editors to view unpublished data in the preview.  The first part was finding out if the current request was being made via the editor or from the website, this was done by checking if the SystemPageBase type had been populated (In edit mode it will be false):

var currentPage = Page as SystemPageBase;
list.DataSource = currentPage == null ? GetEditModePageData() : GetWebViewPageData();

The next step was getting all child pages for a stack page, regardless of published status.  After some Googling I found an article that demo’ed how to get all unpublished child pages

DataFactory.Instance.GetPage(PageReference, LanguageSelector.AutoDetect(true)

However, when you call this method, if a page is unpublished, not all the properties in PageData are correctly populated. From the Epi forum this looks like a known bug.  TO overcome the issue you have to re-iterate over your data to get the latest version. See code below :

private IEnumerable GetEditModePageData()
{
IEnumerable pageData = null;
try
{
// Get all unpublished child pages
var pages = DataFactory.Instance.GetChildren(CurrentPage.PageLink, LanguageSelector.AutoDetect(true));
// Fix EPI missing meta-data bug
pageData = pages.Where(x => x is CompanySectionPageType && x.VisibleInMenu)
.Select(x => GetLastVersion(x.PageLink));
}
catch (Exception ex)
{
Logger.Error(ex);
}
return pageData;
}
public static PageData GetLastVersion(PageReference page)
{
PageData pageData = null;
try
{
var latesVersion = PageVersion.List(page).FirstOrDefault(pageVersion => pageVersion.IsMasterLanguageBranch);
if (latesVersion != null)
{
pageData = DataFactory.Instance.GetPage(latesVersion.ID, LanguageSelector.AutoDetect(true));
}
}
catch (Exception ex)
{
Logger.Error(ex);
}
return pageData;
}

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts