EPiServer: Handling unpublished pages

by Stephen Horsfield 1. April 2008 10:45

Overview

I've recently hit an issue where some custom controls have been generating exceptions when unpublished pages exist in an EPiServer site.  In these cases, the relevant (required) properties have null values.

Context

Rather than using the standard EPiServer property controls, these pages use ASP.NET data-binding and custom controls that access the required properties of EPiServer PageData objects.  They are being viewed in the editing interface by a logged-in user with full permissions.  The PageData object is sourced using a FindPagesWithCriteria operation.

Problem

Any of the custom page properties accessed using PageData["PropertyName"] (in C#) return null preventing the display of any useful information and causing exceptions in various cases, such as in sorting operations and date formatting.

Cause

EPiServer does not automatically load the properties for unpublished pages.  You have to manually instruct EPiServer to load them.

Solution

Check to see if the page is unpublished and (optionally) whether the user has appropriate access rights.  If appropriate, load the full version of the unpublished page and possibly store it in a PageDataCollection:

   1: public static PageDataCollection LoadUnpublishedAsNecessary(PageDataCollection pdc, 
bool clearSource)
   2: {
   3:     PageDataCollection pdc2 = new PageDataCollection();
   4:  
   5:     foreach (PageData p in pdc)
   6:     {
   7:         if (p.PendingPublish)
   8:         {
   9:             AccessLevel l = p.QueryAccess();
  10:             if (((l & AccessLevel.Edit) == AccessLevel.Edit) ||
  11:                  ((l & AccessLevel.Publish) == AccessLevel.Publish))
  12:             {
  13:  
  14:                 PageReference r = new PageReference(p.PageLink.ID, true);
  15:                 pdc2.Add(EPiServer.Global.EPDataFactory.GetPage(r));
  16:                 continue;
  17:             }
  18:         }
  19:         pdc2.Add(p);
  20:     }
  21:     if (clearSource) pdc.Clear();
  22:  
  23:     return pdc2;
  24: }

References

Versions

  • EPiServer 5, EPiServer 4.6

Metadata

Tags:

ASP.NET | EPiServer | Software Development

Powered by BlogEngine.NET 1.5.0.7
Theme by Interakting

Interakting

A full service digital agency offering online strategy, design and usability, systems integration and online marketing services that deliver real business benefits and ensure your online objectives are met.

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar