Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve Sitecore media items from media url?

How to retrieve Sitecore media item from the URL we have?

The URL is dynamic URL e.g. /~/media/14BDED00E4D64DFD8F74019AED4D74EB.ashx.

This is generated when you add item in rich text field.

like image 257
user2523109 Avatar asked Jun 26 '13 07:06

user2523109


1 Answers

You can use the code below:

DynamicLink dynamicLink;
if (!DynamicLink.TryParse("/~/media/14BDED00E4D64DFD8F74019AED4D74EB.ashx", out dynamicLink))
    return;
MediaItem mediaItem = Sitecore.Context.Database.GetItem(dynamicLink.ItemId, dynamicLink.Language ?? Sitecore.Context.Language);
like image 200
Marek Musielak Avatar answered Oct 20 '22 16:10

Marek Musielak