In this tutorial, you will learn how to get the current page using the Episerver CMS API. To get access to a page using Episerver, you can use the IContentLoader API, like this:

The code above access the IContentLoader interface to query the CMS. This interface is a read-only API that will allow you to perform a lot of useful read operations from the CMS. To get information about a specific page, use the Get() call. You will need to pass in the page ID that you want to query for. The page ID can be found within the Episerver editor (look in the URL when you load the page).

This approach is using dependency injection to get access to the API. The code above uses something called a service location pattern to get the data, ServiceLocator.Current.GetInstance();. This approach is not the best practice, however, it makes writing code snippets easy to read. This is why you will see service locators used a lot in online tutorials.

You should inject this dependency via the contractor, like this:

In the Episerver V6, a singleton pattern was used, called the DataFactory to access data (see code at the end) . The singleton pattern meant developers had to implement a lot of concrete references all over their codebase, which made 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. In Episerver v7, being able to call the API using an interface rather than a concrete implementation helps us, as developers, to create more flexible and testable systems.

Another thing to note with this API call is that it is not just page-specific, you can use this API to call blocks as well. This is where it is important for you to understand the concept of IContent that has been introduced. Using this API will give you access to objects that implement the IContent interface.

How To Access A Page Using Episerver 6

For completeness (and this post is very old), in Episerver v6 and below you could access a page using the DataFactory, like so:

Just so you extra some extra value from this post, I will provide you with some other useful code snippets:

How to get all child pages

Return all content items, including, the root page:

Compare if two content items are the same:

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

Happy Coding 🤘