Media Url Options

Media Url Options

MediaUrlOptions

  • Sitecore.Resources.Media.MediaUrlOptions is a class for defining URL configuration settings for a media item from code

MediaUrlOptions Properties

  • MaxHeight
  • Thumbnail
  • AllowStretch


Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Items.Item
  • Sitecore.Data.Items.MediaItem
  • Sitecore.Resources.Media.MediaManager
  • Sitecore.Resources.Media.MediaUrlOptions
  • Sitecore.StringUtil

Media URL Options Configuration

  • <configuration>
    • <sitecore>
      • <linkManager defaultProvider="sitecore">
        • <providers>
          • <clear />
            • <add name="sitecore" type="Sitecore.Links.LinkProvider, Sitecore.Kernel" addAspxExtension="true" alwaysIncludeServerUrl="false" encodeNames="true" languageEmbedding="always" languageLocation="filePath" shortenUrls="true" useDisplayName="false" />

Media URL Options C# Web Control Code

Sitecore.Resources.Media.MediaUrlOptions mediaUrlOptions = new Sitecore.Resources.Media.MediaUrlOptions();
mediaUrlOptions.MaxWidth = 350;
mediaUrlOptions.BackgroundColor = System.Drawing.Color.Gold;
mediaUrlOptions.AllowStretch = true;

string mediaItemPath = "/sitecore/media library/Images/TIC/Banners/Monterey069";
Sitecore.Data.Items.Item item = Sitecore.Context.Database.GetItem(mediaItemPath);
if (item == null) return;

Sitecore.Data.Items.MediaItem mediaItem = new Sitecore.Data.Items.MediaItem(item);
string mediaUrl = Sitecore.Resources.Media.MediaManager.GetMediaUrl(mediaItem, mediaUrlOptions);
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();

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);
//using &#58; for colon and &#160; for space
output.Write("{0}&#58;&#160;{1}", "Media Item", mediaItem.DisplayName);
output.RenderEndTag(); //P

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Href, imageUrl);
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.A);
output.Write("{0}", imageUrl);
output.RenderEndTag();  //A
output.RenderEndTag(); //Div