In this tutorial, you will learn how to get access to the CurrentPaget object within Episerver CMS V8. In Episerver 8, there are two API's that you can use to fetch and modify content from the CMS. These are the IContentLoader and the IContentRepository. In today's guide, you will learn the differences between these two APIs. If you want to be an Episerver querying ninja, read on 🔥🔥🔥

IContentLoader

IContentLoader is the basic API for querying content from the CMS. This API can be used if you want to read data from the CMS only. You can not do any updates or save operations using this API. It implements methods such as Get<T>, GetChildren<T> and GetDescendants<T>. To get a page from Episerver using IContentLoader you would use this code:

The API uses generics, where T is the type of content you want to query from the CMS. T is passed into Get<T>. In the example above, I have set T as PageType. PageType is a custom class that you will need to define in your code. The only restriction on this code is that T has to be a type that implements from the IContent interface (more information about IContent can be found here.

To aid in your understanding, I have also included an example of how the code to create PageType could look like:

The important part of this code is that the class implements from PageData. PageData implements the IContent interface. If you added this code within your project, built and ran your solution, a new page type called Custom Page Type could be created within the CMS.

IContentRepository

IContentRepository is the more advanced older brother of IContentLoader. IContentRepository provides exactly the same functionality as IContentLoader. It also provides some additional things, like saving and deleting content 😲. Behind the scenes, IContentRepository implements the IContentLoader interface. This is why anything that you can do in IContentLoader you can also do in IContentRepository. Accessing IContentRepository is exactly the same as accessing IContentLoader, as seen here:

An example of how to save some content into Epsierver with IContentRepository looks like this:

When I write code, I tend to just use IContentRepository as you can never plan how the code base will grow over time. If you use IContentLoader and you need to write data back to the CMS, you have more code to refactor and more unit tests to change. The arguments for IContentLoader are usually security and performance. I personally think these are a non-issue and Episerver could have just given us one API. If someone can update your code, you have bigger issues than which API to use. In terms of performance, you use dependency injection to access these APIs which uses a transient context, so API access time is a non-issue. Use the method that makes you happy 😊. Happy Coding 🤘