If you have a sublayout that needs to get the URL of a media item, such as an image (item[“Image”]), here is a quick tip on how you can get that media URL in Sitecore.

 

Item item = Sitecore.Context.Item;

Sitecore.Data.Fields.ImageField imgField = ((Sitecore.Data.Fields.ImageField)item.Fields["Image"]);

string url = Sitecore.Resources.Media.MediaManager.GetMediaUrl(imgField.MediaItem);

string altText = imgField.Alt;

 

Questions?

We are California’s leading provider of Sitecore development services. As a Sitecore Gold Partner and Honorable Mention recipient at the 2020 Sitecore Experience Awards, we understand how to get the most out of the Sitecore platform, and deliver a project that will succeed in the long-term. Feel free to reach out to us if you have questions or need assistance with your Sitecore upgrade.