EPiServer  /  CMS April 11, 2011

Automatically organize EPiServer pages – Part 3

In a previous post I’ve described a way to automatically organize EPiServer pages in various types of structures. I’ve made this code downloadable from GitHub. We’ve also, in the second part of this series of posts, seen how to use this functionality to create structures based on creation date, page name and page type. While that pretty much covers what I have to offer there’s one interesting detail missing, the ability to create composite structures.

Create structures by both page type and date

In the previous post we saw how to group pages by page type by inheriting from PageTypeStructureBase and how to group them by date by inheriting from DateStructureBase. But what about if we want to group by both, first by page type and then by date? You might remember from the first post that the module doesn’t only check if the parent page implements IOrganizeChildren but also checks if the returned parent also implements that interface, and if so if that returned parent does the same, and so on.

In other words we can easily create a page type that will group it’s children first by page type and then by date by using one that groups by date for container pages. Utilizing the page types we created in the second post we can do something like this:

[PageType]
public class TypeAndDateStructure 
    : PageTypeStructureBase<DateStructure>
{
    protected override string GetContainerPageName(
        PageData childPage)
    {
        return base.GetContainerPageName(childPage) + "s";
    }
}

Assuming we have a page type named Article and another named Review we might then end up with a structure looking like this:

ArticleAndReview

Structuring pages alphabetically in two levels

Another scenarios is that we have a lot of pages that we want to structure alphabetically. Since EPiServer doesn’t perform well when a single parent have more than a few hundred children we might then need to group them first by the first letter in their names and then by the second letter in their names. This could be achieved by first creating a page type that groups its children by the second character in their names.

public class SecondCharacterAlphabeticalStructure 
    : AlphabeticalStructureBase<Container>
{
    public override PageReference GetParentForPage(
        PageData page)
    {
        if (page.PageName.Length < 2)
        {
            return PageLink;
        }

        return base.GetParentForPage(page);
    }
    protected override string GetContainerPageName(
        PageData childPage)
    {
        return childPage.PageName[1].ToString().ToUpperInvariant();
    }
}

We can then create the page type for the top level structure by using the above for container pages.

[PageType]
public class TwoLevelAlphabeticalStructure 
    : AlphabeticalStructureBase<SecondCharacterAlphabeticalStructure>
{
}

That’s it

That’s all I have for now in terms of automatically structuring pages and this prototypical module. All kinds of feedback is welcome as long as it’s positive ;-)

PS. For updates about new posts, sites I find useful and the occasional rant you can follow me on Twitter. You are also most welcome to subscribe to the RSS-feed.

Joel Abrahamsson

Joel Abrahamsson

I'm a passionate web developer and systems architect living in Stockholm, Sweden. I work as CTO for a large media site and enjoy developing with all technologies, especially .NET, Node.js, and ElasticSearch. Read more

Comments

comments powered by Disqus

My book

Want a structured way to learn EPiServer 7 development? Check out my book on Leanpub!

More about EPiServer CMS