Try our conversational search powered by Generative AI!

Dan Matthews
May 7, 2013
  6965
(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
Why C# Developers Should Embrace Node.js

Explore why C# developers should embrace Node.js especially with Optimizely's SaaS CMS on the horizon. Understand the shift towards agile web...

Andy Blyth | May 2, 2024 | Syndicated blog

Is Optimizely CMS PaaS the Preferred Choice?

As always, it depends. With it's comprehensive and proven support for complex business needs across various deployment scenarios, it fits very well...

Andy Blyth | May 2, 2024 | Syndicated blog

Adding market segment for Customized Commerce 14

Since v.14 of commerce, the old solution  for adding market segment to the url is not working anymore due to techinal changes of .NET Core. There i...

Oskar Zetterberg | May 2, 2024

Blazor components in Optimizely CMS admin/edit interface

Lab: Integrating Blazor Components into Various Aspects of Optimizely CMS admin/edit interface

Ove Lartelius | May 2, 2024 | Syndicated blog