Pages

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. 

Let's say we have the below HTML content. 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.
<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s. 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>
To generate the dynamic url, we can use the below method. These dynamic URLs will be rendered with friendly URL by Sitecore. 

For Sitecore Item:
LinkUrlOptions options = new LinkUrlOptions();
var dynamicUrl = LinkManager.GetDynamicUrl(item, options);
For Media Item:
var dynamicMediaUrl = MediaManager.GetMediaUrl(item, MediaUrlOptions.GetShellOptions());
Output:
<a title="centuries" href="~/link.aspx?_id=33B86674A94E4F2BA8B4576E606AFB45&amp;_z=z">centuries</a>
<a title="Image" href="-/media/dba85a49980e4e33916acbf798ce21fc.ashx">Image</a>

No comments:

Post a comment