In many situations we need to find out what ID a page type has. One such example is when programmatically creating a new PageData object. The code usually looks something like this:
int pageTypeID = ??? PageData newPage = DataFactory.Instance.GetDefaultPageData(CurrentPage.PageLink, pageTypeID); DataFactory.Instance.Save(newPage, SaveAction.Publish);
So, how do we set the pageTypeID variable? Traditionally we’d retrieve the page type ID from a property on the start page, from appSettings or just hard code it. While that works it’s not exactly pretty and we move something that I think belongs in the code out of the code, making the code harder to understand.
Luckily, Page Type Builder has a class named PageTypeResolver that can help us. PageTypeResolver keeps track of the page types that we have declared in code and their corresponding IDs in the database. Using the GetPageTypeID method in PageTypeResolver we can rewrite the above code into
int pageTypeID = PageTypeResolver.Instance.GetPageTypeID(typeof(MyPageType)).Value; PageData newPage = DataFactory.Instance.GetDefaultPageData(CurrentPage.PageLink, pageTypeID); DataFactory.Instance.Save(newPage, SaveAction.Publish)
Erik Nordin has an example of how to build a custom GetDefaultPageData method using the GetPageTypeID method on his blog.
Worth mentioning is also that PageTypeResolver has another method, GetPageTypeType, which does the exact opposite of GetPageTypeID. That is, given a page type ID it returns the declaring type.
Type pageTypeType = PageTypeResolver.Instance.GetPageTypeType(42);
- Get child pages filtered by page type
- Dependency Injection with Page Type Builder 1.2
- Developing with Page Type Builder – Advanced Property Access
- A first stab at EPiServer CMS with ASP.NET MVC and Page Type Builder
- How to create a custom EPiServer property
- Strongly typed access to EPiServer’s Page Objects with Page Type Builder
- Developing with Page Type Builder – Using Interfaces and Advanced Inheritance
- Sweet EPiServer Templating with TypedPageList