Accessing entry point page when using custom page providers
This post is about how to retrieve the entry point page reference when the page data object is served from a custom page provider. We have a look at Page Providers in EPiServer CMS, the DataFactory class and finally how to wrap it all up by extending the PageData class.
Page Providers in EPiServer CMS
Custom page providers in EPiServer CMS are used when you need to add pages in EPiServer CMS from a different data source. Note that using custom page providers with EPiServer CMS requires an enterprise license.
Allan Thraen’s Northwind provider which serves pages from the popular sample Microsoft database and the XmlPageProvider are just two examples of when and how to create custom page providers.
Scenario
Your site implements one or more custom page providers. The entry point page, which is a standard EPiServer CMS PageData object, contains data relating to pages served from the custom page provider.
In order to access the data we need to fetch the page reference of the entry point page.
In this sample we use EPiServer CMS 6 and the sample XmlPageProvider which is available for download at EPiServer world.
Page Provider Configuration
We configure two instances of th e XmlPageProvider. The entryPoint setting defines the ID of the page in EPiServer CMS from which our custom pages should be served as child pages. See the image below.
The XmlPageProvider serves a single page beneath each entry point page as seen in the image below.
The DataFactory class
With the XmlPageProvider configured we need to query the virtual pages from which entry point page they are served from. Fortunately we can access the current page provider from the DataFactory class. See sample code below.
1 2 |
// Fetch the PageProvider from the current page DataFactory.Instance.GetPageProvider(CurrentPage.PageLink); |
Extending PageData
To make it easy to access the entry point page from a custom page provider we want to extend PageData to include a method that does just this. See sample code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
namespace DBLOG.EPiCMS6Sandbox.Website.Templates.Extensions { /// <summary> /// This class contains extension methods for the EPiServer PageData class /// </summary> public static class PageDataExtensions { /// <summary> /// Gets the PageReference from a page that is served from a custom page provider /// </summary> /// <param name="page">The PageData from which to query for the custom page provider entry point PageReference</param> /// <returns>PageReference to the entry point page</returns> /// <remarks>If the PageData object is not served from a custom page provider an empty PageReference is returned</remarks> public static PageReference GetEntryPointPageReference(this PageData page) { // Check if the current page is served from a custom page provider if (!page.PageLink.IsRemote()) return PageReference.EmptyReference; // Retrieve the custom page provider from the PageData object var pageProvider = DataFactory.Instance.GetPageProvider(page.PageLink); // Finally return the entry point as a PageReference return pageProvider.EntryPoint; } } } |
With this extension method we can now query CurrentPage which is of the type PageData. See sample code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
protected void Page_Load(object sender, EventArgs e) { // For brevity we'll keep everything in Page_Load PageReference entryPointPageReference = this.CurrentPage.GetEntryPointPageReference(); if (!PageReference.IsNullOrEmpty(entryPointPageReference)) { string output = String.Format("The current page is served from the custom page provider {0} and the entry point page has the ID {1}", CurrentPage.PageLink.RemoteSite, entryPointPageReference.ID); Response.Write(output); } else { Response.Write("The current page is not served from a custom page provider!"); } } |
With the friendly URL rewriter switched off we can see the page ID of the page served by the XmlPageProvider.
Any data that child pages to the custom page provider require, stored on the entry point page, can now be retrieved by using the extension method.
Thanks for reading!