Get Media URL

Get Media URL

Media Item URLs

  • Specify a relative path to the item
  • Get the Media Library Item
  • Get the list of items in the Media Library
  • Get the Path field of the specified media item

MediaManager.GetMediaURL()

  • Static method for getting the friendly URL of a media item

Sitecore.Data.Items.MediaItem

  • Class for instantiating objects that represent media items
  • Media files are not located under Home item
  • Use Sitecore.StringUtil.EnsurePrefix(prefix, url) to ensure path starts with a slash


C# Web Control Code

Sitecore.Data.Items.Item item = this.GetItem();
if (item == null) return;

string mediaItemPath = "/sitecore/media library/Images/TIC/Awards/Color/PeggyMooneyRec";
Sitecore.Data.Items.Item imageItem = Sitecore.Context.Database.GetItem(mediaItemPath);
if (item == null) return;
Sitecore.Data.Items.MediaItem mediaItem = new Sitecore.Data.Items.MediaItem(imageItem);

string mediaUrl = Sitecore.Resources.Media.MediaManager.GetMediaUrl(mediaItem);
char mediaPrefix = '/';
string imageUrl = Sitecore.StringUtil.EnsurePrefix(mediaPrefix, mediaUrl);
if (string.IsNullOrEmpty(imageUrl)) return;

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Web Control Real-time Output");
output.RenderEndTag(); //Div

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
string displayName = Sitecore.StringUtil.GetString(item.DisplayName, item.Name);
//using : for colon and ? for question mark and   for space
output.Write("{0}: {1}", "Item", displayName);
output.RenderEndTag(); //P

output.Write("{0}&#58;&#160;{1}", "Display Name", "<b>" + mediaItem.DisplayName + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Alternate Text", "<b>" + mediaItem.Alt + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Description", "<b>" + mediaItem.Description + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Extension", "<b>" + mediaItem.Extension + "</b><br/>");
output.Write("{0}&#63;&#160;{1}", "File Based", "<b>" + mediaItem.FileBased.ToString() + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "File Path", "<b>" + mediaItem.FilePath + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Icon", "<b>" + mediaItem.Icon + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "ID", "<b>" + mediaItem.ID.ToString() + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Media Path", "<b>" + mediaItem.MediaPath + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Mime Type", "<b>" + mediaItem.MimeType + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Name", "<b>" + mediaItem.Name + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Path", "<b>" + mediaItem.Path + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Size", "<b>" + mediaItem.Size.ToString() + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Title", "<b>" + mediaItem.Title + "</b>");
output.RenderEndTag(); //Div


Sources