1

I'm having issues trying to get the media image item using url/path like /-/media/Infosite/Images/Content/Teasers/relevant-curriculum-small.ashx?modified=20170314020859&hash=3835E0B83346B4B33EB0787E69117CF560457FFA using the dynamiclink method mentioned here. I also tried stripping off the querystring part but of no use.

I'm trying to retrieve the media item height/width programmatically.

FYI - These media links comes from Rich Text Editor fields in Sitecore.

Any help would be appreciated!

  • The method from the link is for Dynamic Links only. Your link is a full media link. You can try replacing /-/media/ with /sitecore/media library/, remove everything starting from the .ashx, call MainUtil.DecodeName on what you received and then try Database.GetItem with the value. But MediaRequestHandler does many other operations so I cannot guarantee that it works. – Marek Musielak Jan 26 '18 at 10:28
  • I had a similar question yesterday. I found I was able to debug and go through the GetMediaUrl function to actually see how the url is constructed. This also has the options for width and height. If you are having issues still retrieving them you can try and append them your self. sitecore.stackexchange.com/questions/9699/… – RReilley Jan 26 '18 at 14:24
  • Thanks @MarekMusielak for the suggestion and it worked. I was doing everything else except calling MainUtil.DecodeName before calling Database.GetItem and that was returning null. – Chandra Sekaran V Jan 26 '18 at 14:44
  • @RReilley - The problem is when the content editor strip off the height and width of Media link inside Rich Text Editor. My custom mediaprovider was returning height/width for all images by default otherwise. – Chandra Sekaran V Jan 26 '18 at 14:46
  • I'm glad I was able to help. Comment converted into an answer. – Marek Musielak Jan 26 '18 at 17:27
2

The method from the link is for Dynamic Links only.

Your media url is a full media link. You can try the code below:

var mediaUrl = "";
var mediaPath = mediaUrl.Replace("/-/media/", "/sitecore/media library/");
if (mediaPath.IndexOf(".ashx") > -1)
{
    mediaPath = mediaPath.Substring(0, mediaPath.LastIndexOf(".ashx"));
}
mediaPath = MainUtil.DecodeName(mediaPath);
var item = database.GetItem(mediaPath);

But MediaRequestHandler does many other operations so I cannot guarantee that it works in every situation.

  • I'm fairly certain that MainUtil.DecodeName performs a direct replacement of the URL against the <encodeNameReplacements> element. This can be problematic if you are mixing spaces and dashes in any portion of the item path (parents, etc.). Sitecore performs extra work beyond DecodeName to account for this if the ItemNameAllowMixingReplacementCharacterssetting is true, which it is by default. May not matter in the OP use-case, but something to look out for. – jrap Jan 29 '18 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.