smithsson68@gmail.com
Jan 5, 2010
  9243
(1 votes)

Saving Page Objects

EPiServer CMS 6 introduces Page Objects. A Page Object is simply a .NET object that is created by the developer and then associated to an EPiServer CMS page via the EPiServer.Core.PageData.PageObjects collection.

Saving Page Objects is a relatively simple task but there a couple of pitfalls so here are some best practice guidelines to follow:

Saving a Page Object for the first time

Create your object as normal:

   1: MyClass obj = new MyClass();
   2: obj.StringValue = "Hello World!";

Add the object to the PageObjects collection for the current page:

   1: PageData pd = null;
   2:  
   3: if (this.CurrentPage.IsModified)
   4: {
   5:     pd = this.CurrentPage;
   6: }
   7: else
   8: {
   9:     pd = this.CurrentPage.CreateWritableClone();
  10: }
  11:  
  12: pd.PageObjects["MyKey"] = obj;

And then save the current page (as the same version)

   1: DataFactory.Instance.Save(pd, 
   2:                 EPiServer.DataAccess.SaveAction.Publish | 
   3:                 EPiServer.DataAccess.SaveAction.ForceCurrentVersion, 
   4:                 EPiServer.Security.AccessLevel.NoAccess);

Updating an existing object requires a modified flag to be set so the DataFactory knows what to update:

   1: PageData pd = null;
   2:  
   3: if (this.CurrentPage.IsModified)
   4: {
   5:     pd = this.CurrentPage;
   6: }
   7: else
   8: {
   9:     pd = this.CurrentPage.CreateWritableClone();
  10: }
  11:  
  12: // It is important to do the cloning first
  13: // as the clone has a different instance of
  14: // the Page Object to the original
  15: // and therefore you will update the 
  16: // wrong version!!
  17:  
  18: MyClass obj = pd.PageObjects["MyKey"] as MyClass;
  19: obj.StringValue = this.SomeTextBox.Text;
  20:  
  21: // Tell the collection something has changes
  22: pd.PageObjects.SetObjectModified(["MyKey");
  23:  
  24: // And Save
  25: DataFactory.Instance.Save(pd, 
  26:                 EPiServer.DataAccess.SaveAction.Publish |
  27:                 EPiServer.DataAccess.SaveAction.ForceCurrentVersion, 
  28:                 EPiServer.Security.AccessLevel.NoAccess);

IMPORTANT: Replacing an existing object will create a new row in the database if your object does not implement EPiServer.Data.Dynamic.IDynamicData as it’s identity cannot be determined.

Removing a Page Object is simple:

   1: PageData pd = null;
   2:  
   3: if (this.CurrentPage.IsModified)
   4: {
   5:     pd = this.CurrentPage;
   6: }
   7: else
   8: {
   9:     pd = this.CurrentPage.CreateWritableClone();
  10: }
  11:  
  12: pd.PageObjects.Remove("MyKey");
  13:  
  14: DataFactory.Instance.Save(pd, 
  15:                 EPiServer.DataAccess.SaveAction.Publish | 
  16:                 EPiServer.DataAccess.SaveAction.ForceCurrentVersion, 
  17:                 EPiServer.Security.AccessLevel.NoAccess);
Jan 05, 2010

Comments

Sep 21, 2010 10:33 AM

When the Page Object is modified , after modification , how is the instruction pd.PageObjects.SetObjectModified(["MyKey") connecting to the modified object "obj" .

Thanks in advance.
Regards,

S.K. SreeRaghavendra
/ S.K. SreeRaghavendra

smithsson68@gmail.com
smithsson68@gmail.com Sep 21, 2010 10:33 AM

The variable 'obj' was obtained by accessing the PageObjects collection using the key 'MyKey'.

As the MyClass instance is not aware in any way of the PageObjects collection it cannot tell the DataFactory that is needs re-saving because one of it's properties (StringValue) has been updated. Therefore you need to tell the PageObjects collection that one of its object has been modified so it will know it has to save it down to the Dynamic Data Store when the DataFactory.Save method is called for the PageData instrance.

Sep 21, 2010 10:33 AM

Thanks Paul for the reply.


In the code as specified above obj's Property StringValue is being changed.Then in the statement " pd.PageObjects.SetObjectModified(["MyKey");" the collection is indicated of some changes .In the Save method thereafter the Pagedata object "pd" is passed .But there is no statement where the changed object "obj" is again set in the PageObject's Collection.i.e after the statement obj.StringValue is executed.

Kindly clarify my doubt..
Thanks again..


/ S.K. SreeRaghavendra

Sep 21, 2010 10:33 AM

Hi,

This is because by calling

MyClass obj = pd.PageObjects["MyKey"] as MyClass;

you hold a reference to the same physical object still in the PageData.PageObject collection and therefore when you save the PageData the DataFactory looks for any modified flags on Page Objects (which it will find because of the SetObjectModified call) and then save the object back to the Dynamic Data Store.
/ Paul Smith

Sep 21, 2010 10:33 AM

Ok, so when the object "obj" is changed,the pageObject Collection also observers a change and to commit that change ,SetObjectMethod does the rest of the work.

Cool,.
Thank you very very much for your prompt response.
/ S.K. SreeRaghavendra

Please login to comment.
Latest blogs
Optimizely SendGrid SMTP host is deprecated

SendGrid is a services for sending email that is included in Optimizely DXP. Previously smtp.episerver.net was the recommended SMTP server to use,...

Tomas Hensrud Gulla | Dec 4, 2022 | Syndicated blog

Hosting Optimizely CMS 12 on Docker Engine

Since Optimizely CMS can now be deployed as a Docker container, here is a demonstration of building, running and scaling an Optimizely CMS 12 site ...

Stefan Holm Olsen | Dec 4, 2022 | Syndicated blog

How to use CacheTagHelper with content areas in Optimizely CMS 12

I might be going out on a limb here - if you have a better solution, feel very free to share it!  Upgrading your Optimizely web application from .N...

Andreas J | Dec 2, 2022

The 1001st Piece in your 1000 Piece Puzzle: .NET Default Interface Functions

I was recently working with a client who wanted a reasonably large subsystem added to Optimizely that would add automated management to their...

Greg J | Nov 28, 2022 | Syndicated blog

Video Demonstration, creating a CMS12 Alloy Sample Site

Hey All Below you will find a quick video demonstration on how to install a local version of Alloy Sample based on CMS12 / .Net 6. As you will see ...

Minesh Shah (Netcel) | Nov 28, 2022

Opticon Deep Dive: Optimizely's Product Announcements

We attended Opticon 2022 in San Diego which explored the future of orchestration, experimentation, and growth. We dive into Optimizely's product...

Liz Spranzani | Nov 28, 2022 | Syndicated blog