Try our conversational search powered by Generative AI!

Dan Matthews
May 7, 2013
  6963
(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;

    criterias.Add(criteria);

    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

Comments

Please login to comment.
Latest blogs
Azure AI Language – Extractive Summarisation in Optimizely CMS

In this article, I demonstrate how extractive summarisation, provided by the Azure AI Language platform, can be leveraged to produce a set of summa...

Anil Patel | Apr 26, 2024 | Syndicated blog

Optimizely Unit Testing Using CmsContentScaffolding Package

Introduction Unit tests shouldn't be created just for business logic, but also for the content and rules defined for content creation (available...

MilosR | Apr 26, 2024

Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024