Archive for January, 2012

Extension for getting friendlyUrl when all you have is the internal url in EpiServer

January 23rd, 2012

Here’s a simple extension method for EPiServer when you need to get the friendly url from an internal url.

Note the last line checking if the Uri is an absoluteUri first. If you try to get the AbsoluteUri if it’s not you will get the following exception:
InvalidOperationException: This operation is not supported for a relative URI. This operation is not supported for a relative URI.

?View Code CSHARP
1
2
3
4
5
6
public static string GetFriendlyURL(this string internalURL)
{
   var url = new UrlBuilder(internalURL);
   EPiServer.Global.UrlRewriteProvider.ConvertToExternal(url, null, System.Text.UTF8Encoding.UTF8);
   return url.Uri.IsAbsoluteUri ? url.Uri.AbsoluteUri : url.Uri.OriginalString;
}

Get typed pagedata by page guid in EPiServer CMS

January 3rd, 2012

In EpiServer you might sometimes need to get PageData, preferably typed, from a page guid.

Here is a simple method for getting a page only by sending a guid as an argument, which we can do thanks to the PermanentPageLinkMap class, and results of other pagetypes than the requested are ignored, as well as any permanent links which are file links.

?View Code CSHARP
1
2
3
4
5
6
7
8
9
10
11
12
13
public static T GetPage<T>(Guid pageGuid) where T : PageData
{
    var linkMap = EPiServer.Web.PermanentLinkMapStore.Find(pageGuid) as PermanentPageLinkMap;
    if (linkMap != null)
    {
        var page = DataFactory.Instance.GetPage(linkMap.PageReference);
        if (page != null && page is T)
        {
            return (T)page;
        }
    }
    return null;
}

Tested in EPiServer CMS 6, but should work fine in version 5 as well.