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.
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.
- Page Type Builder 0.8 released – Finally!
- Page Type Builder 1.3.1 released – Some fixes for R2
- Dependency Injection with Page Type Builder 1.2
- How EPiServer CMS caches PageData objects
- Get child pages filtered by page type
- A plea to the EPiServer backend team
- How to create a custom EPiServer property
- EPiAbstractions.Opinionated – A wrapper for EPiServer CMS and Page Type Builder