Try our conversational search powered by Generative AI!

Dan Matthews
May 7, 2013
  6961
(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
Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog

Azure AI Language – Abstractive Summarisation in Optimizely CMS

In this article, I show how the abstraction summarisation feature provided by the Azure AI Language platform, can be used within Optimizely CMS to...

Anil Patel | Apr 18, 2024 | Syndicated blog

Fix your Search & Navigation (Find) indexing job, please

Once upon a time, a colleague asked me to look into a customer database with weird spikes in database log usage. (You might start to wonder why I a...

Quan Mai | Apr 17, 2024 | Syndicated blog

The A/A Test: What You Need to Know

Sure, we all know what an A/B test can do. But what is an A/A test? How is it different? With an A/B test, we know that we can take a webpage (our...

Lindsey Rogers | Apr 15, 2024

.Net Core Timezone ID's Windows vs Linux

Hey all, First post here and I would like to talk about Timezone ID's and How Windows and Linux systems use different IDs. We currently run a .NET...

sheider | Apr 15, 2024