How to get a page in EPiServer 7 ?

 How To Get An EPiServer Page Via the API

In EPi7 you can still use the traditional call to the API, however, this way isn’t classed as best practice anymore.

var startpage = DataFactory.Instance.Get<PageData>(ContentReference.StartPage);

In EPi 7 the best practice approach is:

var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
var startpage = contentLoader.Get<PageData>(ContentReference.StartPage);

If you are asking yourself why the second snippet is better, I’ll explain. In the original version 6 API a singleton pattern was used, called the DataFactory, to access data . This meant developers had to implement a lot of concrete reference all over their code base, which makes unit testing a massive pain in the bum.  As I’m sure most developers know, working towards SOLID principles is a desirable design concept as it allows you to swap out or moc key bits of functionality that can’t be unit tested. Working with abstractions rather than concrete implementations helps us, as developers, to create more flexible and testable systems. With the second method we call the abstraction (the interface) and not the concrete implementation. This, in theory, would allow you to inject the interface into your repository and create a testable class.

Secondly, the CMS no longer only works with just pages but instead the more general content concept (IContent) has been introduced that allows developers access to a whole range of content. This means that in EPi7 you now have access to objects that may not be page objects anymore. Using the second approach provides more flexibility to cater for different types of content in the future.

Below lists some other useful API calls for quick reference

How to get all child pages

var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
var topLevelPages = contentLoader.GetChildren<PageData>(ContentReference.StartPage);

Return all content items up to, and including, the root page

var currentBranch = contentLoader.GetAncestors(contentLink).Select(x => x.ContentLink);

Compare if two content items are the same

if (ContentReference.StartPage.CompareToIgnoreWorkID(contentLink))
{
}

Do you know any other useful Epi API calls ? Leave your most used snippets in the comment section below

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

1 reply

Trackbacks & Pingbacks

  1. […] In EpiServer 8 we have two new EpiServer APIs for fetching and modifying content. These are the IContentLoader and the IContentRepository. In todays guide I’m going to covoer how to do some basic API calls and explain the difference between the two. This article is a follow on from How to get a page in EPiServer 7? […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *