EPiServer CMS Page Object List Helper

In EPiServer

This blog post is about using the PageObjectListBase-class to access a strongly typed list of Page Objects in EPiServer CMS 6 in conjunction with PageTypeBuilder. Page Objects are a nice way to work with strongly typed objects that we want to associate with our pages.

Sample project setup

The sample website well be working with implements the public templates project that ships with EPiServer CMS6. We’ve added PageTypeBuilder in order to define our page types.

Using PageTypeBuilder we’ll create a very simple page type with a CommentList-property to house user comments. The user comments should be strongly typed objects and specific for our instance of a page. For this we’re going to utilize Page Objects functionality.

With our sample project we want to achieve the following:

  • Strongly typed list of Comment-objects accessed through CurrentPage
  • Strongly typed Add and Remove functionality for comments

Inheriting from the base class

In order to implement strongly typed access to our list of objects we’ve created the base class PageObjectListBase<T> to support this behavior. We inherit from this class and specify the type of objects its internal list can contain.

Let’s have a look at the implementation of the CommentList and Comment classes.

The Comment-class implementation looks as follows:

The CommentList-class that inherits from PageObjectListBase<Comment> will be added as a property to the PageType-definition and from the CommentList container class we can access our Comment-objects.

Initialization

The CommentList-property needs to be initialized before it’s accessed. There are two ways we can accomplish this: by modifying the body of the get accessor in the page type or by implementing initialization-functionality in the page template.

In my example I opt for housing the initialization in the page template as shown in the code below.

And the initialization of the CommentList occurs in the Blogpost page template’s OnInit-event.

Accessing the Page Objects from CurrentPage

Since CommentList was defined on the Blogpost page type we can access it through CurrentPage in the implementation of our Blogpost page template.

I’ve left out the Blogpost page template implementation for brevity. But here’s a sample of how the functionality described above could be used with our simple blog post page.

Page Object List Helper

Under the hood

The PageObjectListBase-class uses the type name of the inheriting class when saving and loading our object with the PageObjectManager-class.

The Items-property is of course our generic list of objects with the type of T. This property gets initialized when accessing the property in the body of the get-accessor. The Add, Remove, ClearAll methods operates on the Items-property.

When calling the Save-method the current object (our type inheriting from PageObjectListBase) gets saved.

Download the code

The code for the PageObjectListBase-class is available for download here.

daniel
daniel
Developer
Recent Posts
  • Pankaj

    The link to download the code does not work, can you please fix that.

Contact Us

We're not around right now. But you can send us an email and we'll get back to you, asap.

Not readable? Change text. captcha txt

Start typing and press Enter to search