smithsson68@gmail.com
Feb 18, 2010
  10726
(2 votes)

Changes to Page Objects API in CMS 6 Release

The upcoming final release of CMS 6 will introduce a couple of small changes in the way you load and save Page Objects compared to the RC1 version.

Following feedback and a few bug fixes we decided that mixing Page Objects with PageData and having to go through the DataFactory to save them was not going to work and was not the cleanest API.

This blogpost explains the RC1 way of doing things. In the rest of this post I will go through how to work with Page Objects in the CMS Release version.

The main class you will be interested in is the EPiServer.Core.PageObjectManager which can be found in the EPiServer.dll assembly. The most likely places you will be working with Page Objects are in a CMS Page Template, a CMS User Control or from Dynamic Content. In all 3 cases the PageData object is available and therefore the PageObjectManager takes a PageData in its constructor (there are other constructers available for other scenarios when PageData is not available). This tells the PageObjectManager to load and save Page Objects in the context of that page.

You can load Page Objects like this:

   1: PageData pageData = // Obtain the PageData instance to work with
   2:  
   3: // Construct a PageObjectManager with the PageData
   4: PageObjectManager pom = new PageObjectManager(pageData);
   5:  
   6: Rating rating = pom.Load<Rating>("Rating");
   7:  
   8: if (rating != null)
   9: {
  10:     // Do something with the rating
  11: }

 

You can also load all Page Objects for the page like this:

   1: PageData pageData = // Obtain the PageData instance to work with
   2:  
   3: // Construct a PageObjectManager with the PageData
   4: PageObjectManager pom = new PageObjectManager(pageData);
   5:  
   6: var objects = pom.LoadAll();
   7:  
   8: // Get the Rating object
   9: Rating rating = objects.ItemAs<Rating>("Rating");
  10:  
  11: if (rating != null)
  12: {
  13:     // Do something with the rating
  14: }

Note that the LoadAll method returns an IDictionary. The ItemAs<> method is an extension method added as part of the Page Objects implementation to obtain the object in a typed way.

Saving a Page Object is also straight forward:

   1: PageData pageData = // Obtain the PageData instance to work with
   2:  
   3: // Construct a PageObjectManager with the PageData
   4: PageObjectManager pom = new PageObjectManager(pageData);
   5:  
   6: Rating rating = pom.Load<Rating>("Rating");
   7:  
   8: // Update the rating or create a new one if null
   9:  
  10: // Save it back
  11: pom.Save("Rating", rating);

There is another overload of Save which takes a OwnerOption. I will go into this in more detail in a later blog post.

What all this means is that if you have already started building sites with CMS 6 RC1 and have used Page Objects you will unfortunately need to make these small codes changes for obtaining and saving Page Objects. If anybody also needs to preserve their Page Object instances between RC1 and the release version then please email me paul smith episerver com and I will provide you will a small script to do this.

Feb 18, 2010

Comments

Sep 21, 2010 10:33 AM

So, are Page Objects orthogonal to versioning of page data now?

Sep 21, 2010 10:33 AM

Well, I'm not totally sure what you mean by orthogonal in context, if you mean independent of each other then the answer is not really.
Page Objects are not versioned as such, there is no magic going on in the background when a page is versioned to copy the Page Objects. When a Page Objects is created it is assigned an OwnerOption with the options being Page, PageLanguageBranch and PageVersion. So you decide how your Page Objects are shared between different versions/languages of a page. I will blog more shortly.
/Paul
/ Paul Smith

Sep 21, 2010 10:33 AM

I like this API a lot better than the previous one. A good change!

Sep 21, 2010 10:33 AM

Will there be a method to get the page data objects based on PageReference instead of pagedata object?

/ Anders Hattestad

Please login to comment.
Latest blogs
Opti ID overview

Opti ID allows you to log in once and switch between Optimizely products using Okta, Entra ID, or a local account. You can also manage all your use...

K Khan | Jul 26, 2024

Getting Started with Optimizely SaaS using Next.js Starter App - Extend a component - Part 3

This is the final part of our Optimizely SaaS CMS proof-of-concept (POC) blog series. In this post, we'll dive into extending a component within th...

Raghavendra Murthy | Jul 23, 2024 | Syndicated blog

Optimizely Graph – Faceting with Geta Categories

Overview As Optimizely Graph (and Content Cloud SaaS) makes its global debut, it is known that there are going to be some bugs and quirks. One of t...

Eric Markson | Jul 22, 2024 | Syndicated blog

Integration Bynder (DAM) with Optimizely

Bynder is a comprehensive digital asset management (DAM) platform that enables businesses to efficiently manage, store, organize, and share their...

Sanjay Kumar | Jul 22, 2024

Frontend Hosting for SaaS CMS Solutions

Introduction Now that CMS SaaS Core has gone into general availability, it is a good time to start discussing where to host the head. SaaS Core is...

Minesh Shah (Netcel) | Jul 20, 2024

Optimizely London Dev Meetup 11th July 2024

On 11th July 2024 in London Niteco and Netcel along with Optimizely ran the London Developer meetup. There was an great agenda of talks that we put...

Scott Reed | Jul 19, 2024