Media Link

Media Link

This snippet uses a fields collection to reference the field
  • Returns null if the field does not exist or the field value is null
  • Does not generate inline editing controls for the Page Editor
  • Useful for field types other than simple field types


C# Web Control Code

Sitecore.Data.Items.Item item = Sitecore.Context.Item;

Sitecore.Data.Fields.FileField field = item.Fields["fieldname"];

if (field != null && field.MediaItem != null)

{

Sitecore.Data.Items.MediaItem media = new Sitecore.Data.Items.MediaItem(field.MediaItem);

string markup = string.Format( @"<a href=""{0}"">{1}</a>", Sitecore.Resources.Media.MediaManager.GetMediaUrl(media), media.Name);

}