How to get Content Type from PageData or ContentData in EPiServer CMS 7

There are times when you need to get the ID of a Content Type (Page Type) in EPiServer CMS. If you are used to developing with PageTypeBuilder you know that you can use the PageTypeResolver.Instance.GetPageTypeID method to get the Page Type ID. In EPiServer CMS 7 this of course does not exist.

The new way is to use the Load method on an instance of IContentTypeRepository. Use the Service Locator or DI to get an instance of it.

var contentTypeRepository = ServiceLocator.Current.GetInstance<IContentTypeRepository>();
var articlePageType = contentTypeRepository.Load<ArticlePage>();
var pageTypeId = articlePageType.ID;

published in EPiServer