Dean
Dean

Reputation: 1084

Sitecore: How to get a Hyperlink Manager Url from a General Link Field

In the sitecore Rich Text Editor, user can highlight some words and insert a hyperlink. User can select a media item and it will generate a url like ~/media/9A3E8962D4364D0A9F98178A9572CC08.ashx

enter image description here

I have a general link field, what code should I write to generate the above Url? I have tried GetMediaUrl and GetItemUrl method, but they both return the file location.

My goal is that I need to get a url from a field which look like ~/media/9A3E8962D4364D0A9F98178A9572CC08.ashx. Should I use a different type of field? The file which the link is pointing to is a pdf.

Upvotes: 1

Views: 824

Answers (1)

Anton
Anton

Reputation: 9961

You should pass custom MediaUrlOptions to get desired URL format:

var mo = new MediaUrlOptions();
mo.UseItemPath = false;
var url = Sitecore.Resources.Media.MediaManager.GetMediaUrl(imageField.MediaItem, mo);

UseItemPath indicates whether item path or item id should be used for URL.

Upvotes: 4

Related Questions