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> <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.