Get URL from an EPiServer PageReference

Retrieving the URL to an EPiServer page seems like a basic thing to do, but it’s not quite obvious…

Referring to a page is normally represented by an instance of the PageReference class, so obviously that contains a public property called Url or something, right?

Wrong!

Instead, this is how to do it (as a bonus I also show how to retrieve the PageReference from a page property):

string url = "";
PropertyData pd = CurrentPage.Property["Shop_CartPage"];
if (pd != null && pd.IsNull == false)
{
	PageReference pageref = (PageReference)pd.Value;
	url = GetPage(pageref).LinkURL;
}

If anyone knows better or more efficient ways to do this, please add a comment to this post 🙂

5 thoughts on “Get URL from an EPiServer PageReference”

  1. Hey there,

    Yeah this is the only way to get the page URL, but to tidy it up a bit I would use the IsValue method of the base classes to check if the page has the property and the property has a value:

    string url = "";
    if (IsValue("Shop_CartPage"))
    {
      PageReference pageref = (PageReference)CurrentPage["Shop_CartPage"];
      url = GetPage(pageref).LinkURL;
    }
    
  2. Thanks for your input. Your variant is indeed a bit tidier, although I personally prefer doing it without having to type the property name twice. If it changes or is spelled wrong there are two lines to modify and it’s easy to miss one of them. Been there, done that 😉

    On the other hand, your condition is simpler which is also important. Hmm…

    See you around, Jeremy. BTW, might you be the Jeremy of http://episervernz.blogspot.com? I’ve visited your site a few times, good work!

    Emil

  3. Sorry for late answer…

    Unfortunately I don’t know the answer to that since I haven’t worked with later EPiServer versions. I’ve been too busy working in non-EPi projects…

    /Emil

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.