EPiServer: Getting the target from a Page Link that can be Internal or External

by Stephen Horsfield 24. January 2008 11:23

Overview

On a Page Type, you can create a property for a web link to a page.  This page might be part of the CMS, or it might be on a different site.  How can you find out how to render it?

Process

First, you need to get the PropertyData for the property.  It might not have a value, but even if it does, the value is just an internal reference to the the page, for example "/templates/MyTemplate.aspx?id=1076".  Not very helpful, is it?

Here's a quick method that you can use to return a PageData if the property references a CMS page, or a string with the URL otherwise.  You'll still need to check to see which one is returned, if either.

Example code

static void GetPagePropertyLink(PageData CurrentPage, string PropertyName, out PageData pTarget, out string uTarget) {

    pTarget = null;
    uTarget = null;

    PropertyData pUrl = CurrentPage.Property[PropertyName];

    if (pUrl == null) return;
    if (pUrl.IsNull) return;

    try
    {
        pTarget = EPiServer.Global.EPDataFactory.GetPage(
            PageReference.ParseUrl(pUrl.ToString()), EPiServer.Security.AccessLevel.Read);
    }
    catch (EPiServer.Core.PageNotFoundException) { }
    catch (ArgumentException) { }

    if (pTarget == null) uTarget = pUrl.Value.ToString();

    return;
}

Versions

Metadata

Tags:

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

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar