Get Media Url From Sitecore Image Field

Xing Liu • 10/17/2014

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.

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;

Xing Liu

Xing Liu is a Sitecore MVP with expertise in all aspects of ASP.NET and SQL Server development across a variety of industries including Government, Banking, Healthcare and Energy. He provides technical thought leadership and consulting to SymSoft and client development teams. Xing has deep expertise in using Sitecore Helix in an Agile delivery model to streamline the construction of complex Sitecore websites, portals and their associated authoring environments. Xing is also a globally recognized photographer with an Instagram following approaching 100,000.

Explore topics:

Other Blog Posts from Xing Liu