Monday, 10 April 2017

Sitecore generate dynamic URL

When we migrate a site from legacy platform to Sitecore, there may be cases where Sitecore Rich Text Editor will have links. These html content with links will have relative URL and it may break if we move the page to different folder in Sitecore. 

Sample HTML Content:
 <p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five <a title="centuries" href="/about-us/centuries">centuries</a>, but also the leap into electronic typesetting, remaining essentially unchanged.</p>

In order to set the links with Dynamic URL, we need to read the HTML content (via HtmlaAgilityPack), find the links, generate the dynamic URL and save it. 

To generate the Dynamic URL, we can use the below method. 

For Sitecore Item:
LinkUrlOptions options = new LinkUrlOptions();
var dynamicUrl = LinkManager.GetDynamicUrl(item, options);

For Media Item:
var dynamicMediaUrl = MediaManager.GetMediaUrl(item, MediaUrlOptions.GetShellOptions());

Transformed Links:
<a title="centuries" href="~/link.aspx?_id=33B86674A94E4F2BA8B4576E606AFB45&amp;_z=z">centuries</a>

<a title="Image" href="-/media/dba85a49980e4e33916acbf798ce21fc.ashx">Image</a>

These dynamic URLs will be rendered with friendly URL by Sitecore. 

2 comments:

Articles

Sitecore 9.3 released!

Sitecore 9.3 has the Solr 8.1.1 support (Sitecore compatibility table is yet to be updated). SIF 2.2.0 Got a new editor - The Horizon edi...