Virtual Happy Hour this month, Jun 28, we'll be getting a sneak preview at our soon to launch SaaS CMS!

Try our conversational search powered by Generative AI!

Dan Matthews
May 7, 2013
(1 votes)

EPiServer 7: Strongly typed page types in FPWC

No, not really :) FindPagesWithCriteria (FPWC) is still the same beast it always was. For those of us who used PageTypeBuilder a lot, you will have worked with the page type resolver that allowed us to – at the very least – turn a strongly typed page type into a page type ID and feed it into FPWC.

EPiServer 7 provider the same feature as well. For example, let’s say we have a method to get all children of type ‘StandardPage’. We use the content type repository to ‘resolve’ the page type ID:

private IEnumerable<StandardPage> GetPosts()
    PropertyCriteriaCollection criterias = new PropertyCriteriaCollection();

    PropertyCriteria criteria = new PropertyCriteria();
    criteria.Condition = CompareCondition.Equal;
    criteria.Name = "PageTypeID";
    criteria.Type = PropertyDataType.PageType;
    criteria.Value = Locate.ContentTypeRepository().Load<StandardPage>().ID.ToString();
    criteria.Required = true;


    var posts = Locate.PageCriteriaQueryService().FindPagesWithCriteria(CurrentPage.ContentLink as PageReference, criterias).Cast<StandardPage>();

    return EPiServer.Filters.FilterForVisitor.Filter(posts).Cast<StandardPage>();

This works, and is as performant as FPWC is, but we have a problem with inheritance. What if we had a ‘DetailPage’ that is inherited from ‘StandardPage’? Because FPWC requires a page type ID, this won’t help us. We’ll only ever get StandardPage items back, never DetailPage ones. We could do two FPWC calls of course, but now we’re getting messy.

So what’s the alternative? In old-style code the temptation would be to grab all the descendants then filter them ‘after the event’. You can do this using LINQ (bearing in mind that it’s actually still enumerating behind the scenes – this is not a highly performant way of doing things). At least the code is clean and we are dealing with real strong types so we respect inheritance – no need for page type IDs.

private IEnumerable<StandardPage> GetPosts()
    return EPiServer.Filters.FilterForVisitor.Filter(Locate.ContentRepository().GetDescendents(CurrentPage.ContentLink).Select(pageRef => GetPage(pageRef as PageReference)).Where(page => page is StandardPage)).Cast<StandardPage>();

However, there is now a better and badder way to do this. Although the Get<T> method doesn’t like you passing the incorrect page type as the generic type, the GetChildren<T> does automagical filtering for you to get the right type (including inherited types that can cast back to it). In essence, it’s doing pretty much what our code above does, but in less code and – probably – more efficiently:

private IEnumerable<StandardPage> GetPosts()
    return EPiServer.Filters.FilterForVisitor.Filter(GetChildren<StandardPage>(CurrentPage.ContentLink)).Cast<StandardPage>();

For more detail on this, you can see the related article in the SDK. Note that in all my examples I’m filtering the results for the visitor – this is an often-overlooked and crucial thing that you must do when doing things via the API! Using a PageList or similar web control, if using Web Forms, will do this filtering for you if you treat it nicely.

May 07, 2013


Please login to comment.
Latest blogs
Copying property values part 2

After publishing my last article about copying property values to other language versions, I received constructive feedback on how could I change t...

Grzegorz Wiecheć | Jun 18, 2024 | Syndicated blog

Enhancing online shopping through Optimizely's personalized product recommendations

In this blog, I have summarized my experience of using and learning product recommendation feature of Optimizely Personalization Artificial...

Hetaxi | Jun 18, 2024

New Series: Building a .NET Core headless site on Optimizely Graph and SaaS CMS

Welcome to this new multi-post series where you can follow along as I indulge in yet another crazy experiment: Can we make our beloved Alloy site r...

Allan Thraen | Jun 14, 2024 | Syndicated blog

Inspect In Index is finally back

EPiCode.InspectInIndex was released 9 years ago . The Search and Navigation addon is now finally upgraded to support Optimizely CMS 12....

Haakon Peder Haugsten | Jun 14, 2024