EPiServer: Null values for saved but unpublished page

If you save a new page in EPiServer (ran into this issue using CMS 5, don't know which other versions it affect) without publishing it and then get the new page, you might expect that you will get the property values as they where set when saved. A fair assumption, but instead you'll get null values.

Consider the following:
// Create a new page of page type id 7 under page id 100 
PageData page = DataFactory.Instance.GetDefaultPageData(new PageReference(100), 9);

// Set values
page.PageName = "My new page";
page.Property["MyDateProperty"].Value = new DateTime(2010, 10, 23);

// Save the new page
DataFactory.Instance.Save(page, SaveAction.Save, AccessLevel.NoAccess);

// Get the newly created page (we assumes its page id)
PageData page2 = DataFactory.Instance.GetPage(new PageReference(101));

// Get the property
object test = page.Property["MyDateProperty"].Value;

The variable test will be null. The solution is to use another constructor for creating the PageReference.

This code will work:
// Get the newly created page
PageData page2 = DataFactory.Instance.GetPage(new PageReference(101, true));

// Get the property
object test = page.Property["MyDateProperty"].Value;

This is because just a PageReference(pageId) won't retrieve the latest working page (if you check the WorkingPageId is 0). By using PageReference(pageId, bool anyVersion) you ask for the latest working page version (the version that was saved most recently). This will get you a version of the page with all the property values just like when you saved it.

Related posts:

Comments

comments powered by Disqus