EPiServer : Getting a Page from an Id

Epi Server 7

var pageId = 1;
var pageRef = new PageReference(pageId);
var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
var page = contentRepository.Get<PageData>(pageRef);

or

var pageId = 1;
var pageRef = new PageReference(pageId);
var contentRepository = ServiceLocator.Current.GetInstance<IContentLoader>();
var page = contentRepository.Get<PageData>(pageRef);

The different between the above two snippets of code is the interface passed into GetInstance. IContentloader was designed for read only tasks and has a much simpler interface definition.  In this case, IContentLoader is the simpler way. If you need to modify and save the page after the Get then you won’t have the methods you need unless you use IContentRepository.

I just tend to use IContentRepository as I haven’t noticed any performance issues with it and it gives me access to more options, so I’m less likely to have to refactor later compared to using IContentLoader.

Epi Server 6

 var pageId = 1;
var pageRef = new PageReference(pageId );
var page = DataFactory.Instance.GetPage(pageRef) as PageType;

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

More Posts