London Dev Meetup Rescheduled! Due to unavoidable reasons, the event has been moved to 21st May. Speakers remain the same—any changes will be communicated. Seats are limited—register here to secure your spot!


Aug 10, 2012
  8135
(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
Routing to a page in SaaS CMS

More early findings from using a SaaS CMS instance; setting up Graph queries that works for both visitor pageviews and editor previews.

Johan Kronberg | Apr 14, 2025 |

Developer Meetup - London, 24th April 2025

Next Thursday, 24th April will be Candyspace 's first Optimizely Developer Meetup, and the first one held in London this year! We've have some...

Gavin_M | Apr 14, 2025

Successful Digitalization for SMEs: How Optimizely One can Revolutionize Your Business Processes

"Achieve digital excellence with Optimizely One: Boost efficiency, delight customers, secure growth." In today's digital world, it's crucial for...

Frank Hohmeyer | Apr 11, 2025

Personalized Optimizely CMS Website Search Experiences Azure AI Search & Personalizer

In the last blog, we discussed Integrating the Optimizely CMS website with Azure AI search. Now let’s take a bit more advanced topic to serve...

Naveed Ul-Haq | Apr 10, 2025 |

Integrating Optimizely CMS with Azure AI Search – A Game-Changer for Site Search

Want to elevate your Optimizely PaaS CMS site’s search capabilities? Azure AI Search could be just the tool you need! In this blog, I’ll discuss......

Naveed Ul-Haq | Apr 9, 2025 |

Opensource release: New Package Explorer for Optimizely CMS

The import/export ".episerverdata" packages have been around as far as I can remember - and even though they might seem a bit outdated, it's still...

Allan Thraen | Apr 9, 2025 |