3

Is there a way to gracefully check whether a page exists in EpiServer CMS 5 (given a pageId integer) without having to catch the PageNotFoundException thrown by

DataFactory.Instance.GetPage(pageReference)

(EpiServer will happily create a PageReference using a non existing pageId).

Surely I can check whether a page exists without throwing an exception or doing a massive loop?

Crab Bucket
  • 6,219
  • 8
  • 38
  • 73
Lee Englestone
  • 4,545
  • 13
  • 51
  • 85

3 Answers3

6

[EPiServer CMS 5 R2 SP2] No, not without bypassing the page cache and that is more expensive than catching the exception.

Fredrik Haglund
  • 2,578
  • 2
  • 17
  • 19
0

I find it nice to do the catching in an extension method:

public static bool TryGetPage(this PageReference pageReference, out PageData pd)
{
    try
    {
        pd = DataFactory.Instance.GetPage(pageReference);
        return true;
    }
    catch (Exception)
    {
        pd = null;
        return false;
    }
}
Johan Kronberg
  • 1,086
  • 7
  • 12
-3

There is a static method of PageReference which should help:

PageReference.IsNullOrEmpty(pageLink)
Phil Peace
  • 733
  • 1
  • 7
  • 20
  • 2
    PageReference.IsNullOrEmpty() only checks if the passed page reference is constructed correctly or not null, not if the acutal page exists or not. – Johan Petersson Feb 12 '13 at 13:34