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. 

No comments:

Post a Comment

Articles

Automatically create new version for Lock & Edit without workflow

Sitecore will create a new version on Lock and Edit if the item is tied to a workflow. For our case, version has to be created even withou...