Finding Page Type ID When Using Page Type Builder

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.

GetPageTypeType

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);

PS. For updates about new posts, sites I find useful and the occasional rant you can follow me on Twitter. You are also most welcome to subscribe to the RSS-feed.

Comments

  1. Kalle Hoppe's avatar

    Kalle Hoppe 1 years ago

    Great! this saved me some extra hoops today!
    Keep on the good work!

  2. Karoline Klever's avatar

    Karoline Klever 1 years ago

    Still loving it when I google something and your blog is the first hit ;) Thanks (again)!

  3. Christoffer's avatar

    Christoffer 2 months ago

    I'm using PageTypeResolver.GetPageTypeID in a method I'd like to unit test. How do I mock PageTypeResolver? Can I set the return value of GetPageTypeID somehow? Right now GetPageTypeID always returns null in the tests. Perhaps there is a nice interface/facade somewhere? :)

  4. Joel Abrahamsson's avatar

    Joel Abrahamsson 2 months ago

    Christoffer, The PageTypeResolver class doesn't implement an interface but it's methods are virtual so given that you use dependency injection you can inject a stub/mock that is either manually rolled (overriding relevant methods) or created with some mocking framework.

Follow me on Twitter

  1. @chraas You might want to use it for complex parts of the site and not use it for simple rendering pages. 2 days ago
  2. @chraas In theory more SOLID, but I'm not sure it's worth the price. 2 days ago
  3. @chraas Be warned that you're introducing one big chunk of complexity with EPiMVP :) 2 days ago
follow me

Latest comments

  1. Berra S wrote "Read your post at http://joelabrahamsson.com/entry/using-xfo..." on PageData objects not returned as typed when using Page Type Builder and FindPagesWithCriteria
  2. Linus wrote "1 up for behaviour being as close as expected as possible!" on A common problem with Page Type Builder and UniqueValuePerLanguage set to false
  3. Joel Abrahamsson wrote "Hi Hans, Could it be that you previously didn't have Page..." on Page Type Builder 2.0 released

About this site

This blog is built with EPiServer Community, EPiServer CMS, ASP.NET MVC and a bunch of other great products. The source code is available for download at the projects page, where you also can read more about this site and my other projects.

read more