How To Get Page Revisions For An Episerver Page

When you work with content in the CMS you may need to work with older versions.  When you use the API's like IContentRepository or IContentLocator they will always return you the latest version of the content.  99% of the time you won't need to worry about different versions,  if you do however, you need to be aware of the IContentVersionRepository. One word of caution about IContentVersionRepository.  Episerver doesn't provide any form of caching when you work with different versions of the CMS.  Episerver will only cache the latest/published version of a page.  If you need to use IContentVersionRepository you need not keep this in mind.  In most situations you will probably only ever use IContentVersionRepository to create some back-end plug-in or feature, but if you find yourself needing to use this in production code then you should look into caching the results as well.

 var contentReference = new ContentReference(1);
 var contentVersionRepository = ServiceLocator.Current.GetInstance<IContentVersionRepository>();</pre>
 var versions = contentVersionRepository.List(contentReference);

 versions.OrderByDescending(x => x.Saved)
 .FirstOrDefault(version => version.IsMasterLanguageBranch);

The code above uses the Lis() method to get all the versions for the content reference that we passed in.  We then filter by the OrderDescending method on the Saved Date to get the latest and then we use IsMasterLanguageBranch to check that the version matches the master language branch. If you are not working in a multi-language environment, you can ignore that bit.

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

Back to top
var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-35662136-1']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + ''; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();