Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Eric
Dec 14, 2012
  5281
(1 votes)

Container pages for EPiServer CMS 6 based on PageTypeBuilder

Remember the new feature called Container-pages that came with CMS 6 R2 edition? If you don’t remember it is ok cause most of the project never use them since PageTypeBuilder did not support that. I say did cause I am not that updated on PageTypeBuilder at the moment. Ler 

But I think they are a nice feature that I also promote at developer courses. When I build new websites and structure the content I tend to use them and in the early days of EPiServer we used ordinary pagtypes for this.  If you do not know what they are I suggest you read the blog post that Linus wrote when this feature were released, http://world.episerver.com/Blogs/Linus-Ekstrom/Dates/2011/3/Container-pages/

So, a quick answer is: A PageType without a template! Ler

They will have a special rendering inside edit-mode looking like the image below and also other nice features that Linus mentioned in his blog post.

clip_image002

Render Container-pages with PageTypeBuilder

The problem with PageTypeBuilder is that by default if you leave the “Filename” empty it will fallback to default.aspx or something like that. So we need to force the website to render specific PageTypes like container pages. This can be done in Global.asax.

Add EventHandlers to Global.asax

We need to add two new eventhandlers to global.asax, these will be added to Application_Start.

protected void Application_Start(Object sender, EventArgs e)
{
    EditPanel.LoadedPage += new LoadedPageEventHandler(EditPanel_LoadedPage);
    DataFactory.Instance.LoadedPage += new PageEventHandler(Instance_LoadedPage);
}

Next we create a class for handling all the PageTypes that should be rendered as ContainerPages.

ContainerPages.cs

Here we use PageTypeResolver from PageTypeBuilder to get our Id:s for our different pagetypes.

public class ContainerPages
{
    //Pagetypes without view mode
    public static readonly int?[] ContainerPageIds = new[]
                       {
    
                           PageTypeResolver.Instance.GetPageTypeID(typeof (MyPageType)),
                           PageTypeResolver.Instance.GetPageTypeID(typeof (OtherPageType))
                           
                       };
}

Next we add some logic to the newly created eventhandlers, so we open global.asax again.

Our code in global.asax

public void EditPanel_LoadedPage(EditPanel sender, LoadedPageEventArgs e)
{
 
    if (e.Page != null && ContainerPages.ContainerPageIds.Any(containerPageId => e.Page.PageTypeID == containerPageId))
    {
        e.Page.HasTemplate = false;
    }
}
 
public void Instance_LoadedPage(object sender, PageEventArgs e)
{
    if (e.Page != null && ContainerPages.ContainerPageIds != null && ContainerPages.ContainerPageIds.Any(containerPageId => e.Page.PageTypeID == containerPageId))
    {
        e.Page.HasTemplate = false;
    }
}

Finally we need to add some code to our PageTypes, telling them that the do not have any Template:

[PageType(Name = "MyPageType", FileName="")]
public class MyPageType : TypedPagedData
{
    public MyPageType()
    {
        HasTemplate = false;
    }
}

That is about it.

Not sure this is the best way or if I am doing anything wrong but it looks like it is working ok. It also might work with only the last piece of code, so please give feedback! Ler

Dec 14, 2012

Comments

valdis
valdis Dec 17, 2012 09:17 AM

I'm not sure if I got the idea correctly, but we are dealing with this by defining that page does not have a template in constructor of PageType class (your's last code fragment).

Eric
Eric Dec 17, 2012 09:29 AM

Yes, as i mentioned I think that is good enough but I came up with that in last minute before I published the post. The solution above has been used before I did that in the constructor of the pagetype. Great comment, exactly what I was looking for :)

Dec 17, 2012 09:35 AM

Having the constructor is basically the simplest implementation. But what is the purpose of the events?

But remember to make Page Template inherit SimplePage or any other class that somehow inherits SimplePage and you will trigger LoadCurrentPage() which in turn is responsible to throw a 404 for your visitor.

Eric
Eric Dec 17, 2012 09:46 AM

The events is based on Linus Ekströms blogpost about container pages as mentioned. As i said that was my first solution and what I was showing was a way to get hold of pagetypeid on a project based on pagetypebuilder and make them as container pages. Therefore I added the last piece of code at the end since iÍ came up with that 1 week after i started the blogpost ;)

Great feedback.

Dec 17, 2012 10:29 AM

I see, but if you want plain Container functionality (and inheriting from correct Page Template base class) I think it's enough with the constructor.

EPiServer seems to handle what fields to show and the 404 based on that since PageTypeBuilder has it's proxy loading tightly woven into EPiServer.

Please login to comment.
Latest blogs
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

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