EPiServer  /  CMS January 02, 2012

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.

Joel Abrahamsson

Joel Abrahamsson

I'm a passionate web developer and systems architect living in Stockholm, Sweden. I work as CTO for a large media site and enjoy developing with all technologies, especially .NET, Node.js, and ElasticSearch. Read more

Comments

comments powered by Disqus

My book

Want a structured way to learn EPiServer 7 development? Check out my book on Leanpub!

More about EPiServer CMS