Lee Crowe
Aug 10, 2012
  7145
(0 votes)

Building Hyperlinks in EPiServer

An issue was raised in one of the projects I was working on yesterday to do with some of the hyperlinks that are rendered on various components within the site.

The issue was mainly to do an editor setting a PageReference property to point to a page that has the “Target frame” set to open in a new window.  This option appears on the Shortcut tab below:


Unfortunately most of the components within the site just render the LinkUrl which has been fine up until now.  The editor expects the link to open a new window if the “Target frame” value is set to “Open the link in a new window” on the page being referenced.

This makes sense but is a potentially large amount of effort to implement throughout the site.

So for anyone else who isn’t aware of this potential pitfall I have created some helper methods for building link URL’s which mayprove useful.

The helper class can be downloaded from here.  But the code within the class is also shown below.

  1: namespace Project
  2: {
  3:     using System;
  4:     using System.Text;
  5:     using System.Web;
  6:     using EPiServer;
  7:     using EPiServer.Core;
  8:     using EPiServer.SpecializedProperties;
  9:     using EPiServer.Web;
 10: 
 11:     public static class WebUrlHelper
 12:     {
 13:         public static string BuildLinkHtml(PageReference pageReference, string text, string title = null, 
 14:             string cssClass = null, bool fullyQualified = false, bool newWindow = false)
 15:         {
 16:             string linkUrl = "#";
 17: 
 18:             if (!PageReference.IsNullOrEmpty(pageReference))
 19:             {
 20:                 PageData pageData = DataFactory.Instance.GetPage(pageReference);
 21:                 PropertyFrame propertyFrame = pageData.Property["PageTargetFrame"] as PropertyFrame;
 22: 
 23:                 if (!newWindow && !string.IsNullOrEmpty(propertyFrame.FrameName) && 
 24:                     string.Equals(propertyFrame.FrameName, "_blank", StringComparison.OrdinalIgnoreCase))
 25:                 {
 26:                     newWindow = true;
 27:                 }
 28: 
 29:                 linkUrl = GetPageUrl(pageData, fullyQualified).ToString();
 30:             }
 31: 
 32:             return BuildLinkHtml(linkUrl, text, title, cssClass, newWindow);
 33:         }
 34: 
 35:         public static string BuildLinkHtml(string linkUrl, string text, string title = null, 
 36:             string cssClass = null, bool newWindow = false)
 37:         {
 38:             if (!string.IsNullOrEmpty(title))
 39:                 title = string.Format(" title=\"{0}\"", title);
 40: 
 41:             if (!string.IsNullOrEmpty(cssClass))
 42:                 cssClass = string.Format(" class=\"{0}\"", cssClass);
 43: 
 44:             return string.Format("<a href=\"{0}\"{1}{2}{3}>{4}</a>",
 45:                 linkUrl,
 46:                 cssClass,
 47:                 title,
 48:                 newWindow ? " target=\"_blank\"" : string.Empty,
 49:                 text);
 50:         }
 51: 
 52:         public static Uri GetPageUrl(PageData pageData, bool fullyQualified = false)
 53:         {
 54:             PageShortcutType propertyLinkType = (PageShortcutType)Enum.Parse(typeof(PageShortcutType), pageData.Property["PageShortcutType"].ToString());
 55: 
 56:             UrlBuilder url = new UrlBuilder(pageData.LinkURL);
 57:             bool getPageUrl = true;
 58:             bool changeHostAndScheme = true;
 59: 
 60:             if (propertyLinkType == PageShortcutType.Shortcut && 
 61:                 pageData.LinkURL.IndexOf("id=", StringComparison.OrdinalIgnoreCase) != -1)
 62:             {
 63:                 string id = pageData.LinkURL.Substring(pageData.LinkURL.IndexOf("id=", StringComparison.OrdinalIgnoreCase) + 3);
 64: 
 65:                 if (id.Contains("&"))
 66:                     id = id.Substring(0, id.IndexOf("&", StringComparison.OrdinalIgnoreCase));
 67: 
 68:                 int pageId;
 69: 
 70:                 if (int.TryParse(id, out pageId))
 71:                 {
 72:                     pageData = DataFactory.Instance.GetPage(new PageReference(pageId));
 73:                     url = new UrlBuilder(pageData.LinkURL);
 74:                 }
 75:             }
 76: 
 77:             if (propertyLinkType == PageShortcutType.External)
 78:             {
 79:                 getPageUrl = false;
 80:                 changeHostAndScheme = false;
 81:             }
 82: 
 83:             if (UrlRewriteProvider.IsFurlEnabled && getPageUrl)
 84:                 Global.UrlRewriteProvider.ConvertToExternal(url, pageData.PageLink, Encoding.UTF8);
 85: 
 86:             if (changeHostAndScheme && fullyQualified && HttpContext.Current != null)
 87:             {
 88:                 url.Host = HttpContext.Current.Request.Url.Host;
 89:                 url.Scheme = HttpContext.Current.Request.Url.Scheme;
 90:             }
 91: 
 92:             return url.Uri;
 93:         }
 94:     }
 95: }

There are two BuildLinkHtml methods that can be called and also a GetPageUrl method.

Hopefully this will prove of some use to other people Smile

Feedback

As always feedback is greatly appreciated. Just twitter me @croweman or send me an email.

Aug 10, 2012

Comments

Dinesh Yadav
Dinesh Yadav Feb 6, 2014 04:58 PM

Hi, I am also looking for similar solution , when we are trying to set Target Frame as "Open in new window" , it opens in the same window.

Please suggest how can i use the above code.

Please login to comment.
Latest blogs
Plug-in manager is back in CMS 12

Plug-in manager is back in the UI, what is it and how can i use it?

Luc Gosso (MVP) | Oct 6, 2022 | Syndicated blog

Display Child Pages in Content Delivery API Response

The below example will implement an instance of IContentConverterProvider to customise the serialisation of PageData and output child pages in the...

Minesh Shah (Netcel) | Oct 4, 2022

Bring the Report Center back in Optimizely CMS 12

The Report Center has been a part of Optimizely CMS since its first debut in version 5R2 in 2008, but in CMS 12, it's removed! Don't despair! Make...

Tomas Hensrud Gulla | Oct 4, 2022 | Syndicated blog

Customizing Property Lists in Optimizely CMS

Generic property lists is a cool editorial feature that has gained a lot of popularity - in spite of still being unsupported (officially). But if y...

Allan Thraen | Oct 2, 2022 | Syndicated blog

Optimizely names Luminary Senior Developer, Ynze Nunnink, OMVP

Luminary Senior Developer and Optimizely Lead, Ynze Nunnink has secured the coveted position of Optimizely MVP. Earning a Platinum badge for his...

Ynze | Oct 2, 2022 | Syndicated blog

Content Delivery API – The Case of the Duplicate API Refresh Token

Creating a custom refresh provider to resolve the issues with duplicate tokens in the DXC The post Content Delivery API – The Case of the Duplicate...

David Lewis | Sep 29, 2022 | Syndicated blog