PageData objects not returned as typed when using Page Type Builder and FindPagesWithCriteria

A question I’ve been seeing and getting quite often recently is why EPiServer pages are returned as plain PageData objects instead of their typed Page Type Builder classes, such as ArticlePage or StartPageData. Although the questions don’t always revolve around the DataFactory method FindPagesWithCriteria it pretty much always has to do with using that. To cut to the chase directly there’s nothing Page Type Builder can do to fix this, but from version 1.3.1 and upwards it ships with an extension method for PageDataCollection named AsTyped that gives users of FindPagesWithCriteria and the GetPages method an easy workaround.

In other words, if you find yourself in a situation where PageData objects aren’t of the type you’d expect try calling AsTyped() on your PageDataCollection object.

Background

The reason why this happens is that a bug slipped in in the R2 version of EPiServer CMS 6. In that version a new method named GetPages (note the s on the end) was added. This method allows us, and EPiServer’s own methods, to retrieve a number of pages in a way that is more effective than calling GetPage multiple times. Quite naturally EPiServer decided to change FindPagesWithCriteria to use this method and that shouldn’t have caused any problems. However, GetPages has a small but important bug.

Like other DataFactory methods that return pages it fires events whenever it loads a page. Normally subscribers to these events can intercept the loaded page and replace it. That’s how PTB is able to make DataFactory return PageData objects of our own custom classes instead of plain old PageData objects. This works fine with GetPages but unlike other DataFactory methods it doesn’t actually check if the page which it is about to return has been changed by event listeners and therefor always returns the original PageData object.

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. Berra S's avatar

    Berra S 4 months ago

    Read your post at http://joelabrahamsson.com/entry/using-xforms-with-page-type-builder and wonder how to populate the xform-property when the page is created programmatically.

Follow me on Twitter

  1. @ErikEngstrand Ah, well, that could possibly cause data to be lost so PTB won't do that for you. You can do it manually or use migrations 1 days ago
  2. @ErikEngstrand Sounds odd. You sure it isn't set to disabled in web.config? 1 days ago
  3. @JakeSpur Mind e-mailing me? mail[at]http://t.co/OHdmCZXX 1 days ago
follow me

Latest comments

  1. Per Ivansson wrote "We will definitely try to release as continiously as we poss..." on On selling 200OK and Truffler to EPiServer
  2. Joel Abrahamsson wrote "Thanks Andreas! Regarding your questions it's not really ..." on On selling 200OK and Truffler to EPiServer
  3. Andreas R wrote "Congrats on the sale. Hope ur rolling around in cash now ;) ..." on On selling 200OK and Truffler to EPiServer

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