World is now on Opti ID! Learn more


Aug 10, 2012
  5468
(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
Make Global Assets Site- and Language-Aware at Indexing Time

I had a support case the other day with a question around search on global assets on a multisite. This is the result of that investigation. This co...

dada | Jun 26, 2025

The remote server returned an error: (400) Bad Request – when configuring Azure Storage for an older Optimizely CMS site

How to fix a strange issue that occurred when I moved editor-uploaded files for some old Optimizely CMS 11 solutions to Azure Storage.

Tomas Hensrud Gulla | Jun 26, 2025 |

Enable Opal AI for your Optimizely products

Learn how to enable Opal AI, and meet your infinite workforce.

Tomas Hensrud Gulla | Jun 25, 2025 |

Deploying to Optimizely Frontend Hosting: A Practical Guide

Optimizely Frontend Hosting is a cloud-based solution for deploying headless frontend applications - currently supporting only Next.js projects. It...

Szymon Uryga | Jun 25, 2025

World on Opti ID

We're excited to announce that world.optimizely.com is now integrated with Opti ID! What does this mean for you? New Users:  You can now log in wit...

Patrick Lam | Jun 22, 2025

Avoid Scandinavian Letters in File Names in Optimizely CMS

Discover how Scandinavian letters in file names can break media in Optimizely CMS—and learn a simple code fix to automatically sanitize uploads for...

Henning Sjørbotten | Jun 19, 2025 |