A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More


Aug 10, 2012
  5501
(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

Please login to comment.
Latest blogs
A day in the life of an Optimizely OMVP: Learning Optimizely Just Got Easier: Introducing the Optimizely Learning Centre

On the back of my last post about the Opti Graph Learning Centre, I am now happy to announce a revamped interactive learning platform that makes...

Graham Carr | Jan 31, 2026

Scheduled job for deleting content types and all related content

In my previous blog post which was about getting an overview of your sites content https://world.optimizely.com/blogs/Per-Nergard/Dates/2026/1/sche...

Per Nergård (MVP) | Jan 30, 2026

Working With Applications in Optimizely CMS 13

💡 Note:  The following content has been written based on Optimizely CMS 13 Preview 2 and may not accurately reflect the final release version. As...

Mark Stott | Jan 30, 2026

Experimentation at Speed Using Optimizely Opal and Web Experimentation

If you are working in experimentation, you will know that speed matters. The quicker you can go from idea to implementation, the faster you can...

Minesh Shah (Netcel) | Jan 30, 2026