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 6 months ago

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

Add a comment

Allowed tags: <b>, <em>, <quote cite="">, <code>, <c-sharp-code>, <css-code>, <sql-code>, <xml-code>, <javascript-code>. If you want to display code examples, please remember to write &lt; for < and &gt; for >.

Follow me on Twitter

  1. Blogged: Learning Scala part nine – Uniform Access http://bit.ly/bmUat8 1 days ago
  2. @mikaellundin Your blog is a never ending source of wisdom. And weird mathematical problems. :) 3 days ago
  3. Bookmarked: Validate XHtml 1.0 Strict as part of your build process « Mint http://bit.ly/bOhaZj 3 days ago
follow me

Latest comments

  1. Svante wrote "Yes, I noticed that it was a singleton, and I guess the real..." on Something to beware of when using EPiAbstractions and an IoC container
  2. Joel Abrahamsson wrote "Well, first of all you wont get any arguments from me regard..." on Something to beware of when using EPiAbstractions and an IoC container
  3. Svante wrote "Hmm... Since the issue really is with the public instance co..." on Something to beware of when using EPiAbstractions and an IoC container

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