How To Get The Current Page In Episerver 8?

In Episerver 8 we have two API’s available for us to allow fetching and modifying content. These are the IContentLoader and the IContentRepository.  In today’s guide, I’m going to cover how to do some basic API calls with Episerver and hopefully explain the difference between the two.

This article is a follow on from How to get a page in EPiServer 7?

IContentLoader

IContentLoader is the basic API that should be used if you only ever need to do read operations.  It implements methods such as Get<>, GetChildren<> and GetDescendants<>. To get a page from Episerver, you would use this code:

var repository = ServiceLocator.Current.GetInstance<IContentLoader>();
var contentReference = new ContentReference();
var epiServerContent = repository.Get<PageType>(contentReference);

In this example, the Type being passed into the Get<> method, is a custom class that you will need to define in your code.  This type can be swapped out for any class that implements the IContent interface.  More information about IContent, can be found in Different Ways Of Casting Objects From The Episerver Api

A quick example of how a class definition for a PageType might look like, is shown below:

[ContentType(
DisplayName = "Custom Page Type",
AvailableInEditMode = true,
GUID = "7db1e6e7-81d5-4848-ac01-415dd31fdb59",
Description = "Creates a custom conten page.")]
[ImageUrl("./image/custom_page_type_image.png")]
public class PageType: PageData
{
}

IContentRepository

IContentRepository is the more beefy cousin of IContentLoader. IContentRepository provides the same functionality as IContentLoader and also allows us to do exciting things like.. saving and deleting content… WOW 😛 In technical terms IContentRepository implements the IContentLoader interface, so anything you can do in IContentLoader you can also do in IContentRepository.

IContentRepository works very similarly as IContentLoader, as seen here:

var repository = ServiceLocator.Current.GetInstance<IContentRepository >();
var contentReference = new ContentReference();
var epiServerContent = repository.Get<PageType>(contentReference);

An example of saving content might then look like this:

var repository = ServiceLocator.Current.GetInstance<IContentRepository >();
var newPage= baseDependencies.ContentRepository
.GetDefault<PageType>(ContentReference.StartPage);
repository .Save(newPage, SaveAction.Publish, AccessLevel.NoAccess);

Conclusion

Episerver provides several easy to use API’s for us to work with content.. specifically IContentLoader and IContentRepository.  When I write code, I tend to just use IContentRepository as you can never plan how the code base will grow over time.

The IContentLoader API provides developers with a read-only way to talk to Episerver. You may want to only use this in your view model code for security, while the IContentRepository gives read and write access.

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

3 replies
  1. Scott Reed
    Scott Reed says:

    I’ve been using both these in one of my recent builds. Personally I’ve opted for using IContentLoader as my default across the board as in my training early in the year with the updated version they suggested that due to the extra overhead of the IContentRepository. At the end of the day if you find yourself needing to write instead it’s pretty darn easy to swap out

    Reply
  2. Dave
    Dave says:

    Some great information on these articles, really helpful.

    My only gripe is that 3 articles in I am seeing nothing but spelling mistakes and missing words.

    conclussion is a big bold word that should be conclusion.

    “PageTYpe”

    “class that has defined in your code.”

    I don’t mean to be petty I am just finding it very hard to read through these with such annoying occurances. Perhaps a proof read and clean up wouldn’t go amiss! 🙂 it’s very offputting.

    Reply

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 *