In this tutorial, you will learn how to get a previous version of an Episerver page using the power of code. When you use API's like IContentRepository or IContentLocator, by default it will always return the latest version of a page. This makes sense as 99% of the time you will want to work with the latest changes. When you want to work with older data you need use a different API, read on to learn which one 💥

To work with previous versions of content, you need to use the IContentVersionRepository API. The code to use this API is shown below:

This code uses the List() method to get all the versions for that content. You can then use OrderByDescending() on the SavedDate property to get the latest version. If you are not working in a multi-language environment, you can use IsMasterLanguageBranch to check that the version matches the main language branch.

You need to be careful when using IContentVersionRepository as Episerver doesn't provide any form of caching under the hood with this API. In most situations, you will probably only ever use IContentVersionRepository to create some back-end plug-in or feature. If you find yourself needing to use this API on a webpage, be careful!

You now know how to get previous versions of a page. Use the API wisely. Happy Coding 🤘