In this tutorial, you will learn how to query for a page from the CMS in code using Episerver CMS. You will learn how to do this for using Episerver v6 and Episerver v7. Sound good then read on 🔥🔥

Episerver 7: There are two ways to query for a specific page using Episerver v7. The first is shown below:

The alternative approach is:

The difference between the above two snippets of code is the interface that is passed into GetInstance(). IContentLoader was designed for read-only tasks. IContentRepository provides read and write only capabilities. As IContentLoader only provides read-only abilities, it exposes fewer methods.

If you need to modify and save the page after the Get() call, won't have the methods available to you if you use IContentLoader. In these situations use IContentRepository. I recommend that you always use IContentRepositoty as you will have less code to refactor later on. If you decide you need to do a write operation within your code, you will have to swap IContentLoader for IContentRepository causing you more work.

Episerver 6: Querying for the current page is done n a different way within Episerver V6:

This approach uses the DataFactory. This singleton API can not be used with dependency injection. This is the main reason why it was replaced. If you are working in v6 this is your only option. Happy Coding 🤘