Resolve PageType & ContentType from Class

In PageTypeBuilder for EPiServer CMS 6 we had a lovely feature called PageTypeResolver which could be used to find the Id of a PageType based on your implementation of TypedPageData. All you needed to do is run PageTypeResolver.Instance.GetPageTypeID(typeof(MyPageType)) and you would get a Nullable Int which represents the Id of your PageType, or null if no PageType could be found. In EPiServer 7 CMS this has been built into the main functionality of Page Types / Content Types and all you need to do is this:

  • Find the implementation of IContentTypeRepository you’re looking for:
    var repository = ServiceLocator.Current.GetInstance<IContentTypeRepository<PageType>>();
    var repository = ServiceLocator.Current.GetInstance<IContentTypeRepository<BlockTypeType>>();
    var repository = ServiceLocator.Current.GetInstance<PageTypeRepository>();
    var repository = ServiceLocator.Current.GetInstance<BlockTypeRepository>();
  • Load the page type based on your implementation of BlockData/PageData:
    var contentType = repository.Load(typeof(MyPageType));

If you’re locating the PageTypeRepository or BlockTypeRepository implementations directly you can also find the shortcut:

var contentType = repository.Load<MyPageType>();

If you try to load an instance of ContentData that does not exist, Load() will return null.

2 thoughts on “Resolve PageType & ContentType from Class

  1. This helped me today. Just to let you know :D

Leave a comment