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

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
Optimizely Opal: How to Build Effective Workflow Agents

If you're building workflow agents in Optimizely Opal, this post covers how specialized agents pass context to each other, why keeping agents small...

Andre | May 20, 2026

ReviewPR: An Azure Function That Reviews Your Azure DevOps Pull Requests With Claude

A while back I wrote about an  Azure Function App for PDF creation that we use to offload PDF rendering from our Optimizely DXP site. That same...

KennyG | May 19, 2026

Accelerating Optimizely CMS and Commerce upgrades with agentic AI (Part 2 of 2)

The Real Transformation in Optimizely CMS 13: Why the Upgrade Itself Is the Easy Part. A field-tested playbook for enterprise teams moving from...

Hung Le Hoang | May 18, 2026

Is the most powerful AI model really the best value?

Artificial Intelligence is already becoming part of everyday software development. Developers now use AI tools to generate code, write documentatio...

K Khan | May 16, 2026