Get Media Path

Get Media Path

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Database
  • Sitecore.Data.Items.Item

Media Path C# Web Control Code

string mediaItemPath = "/sitecore/media library/Images/TIC/Banners/Monterey069";
Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item item = db.GetItem(mediaItemPath);
if (item == null) 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 : for colon and ? for question mark and   for space
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

string mediaPath = item.Paths.Path;
string mediaUri = item.Uri.ToString();

if (!string.IsNullOrEmpty(mediaPath))
{
    output.Write("{0}&#58;&#160;{1}", "Media Path", "<b>" + mediaPath + "</b><br/>");
    output.Write("{0}&#58;&#160;{1}", "Media URI", "<b>" + mediaUri + "</b>");
}
output.RenderEndTag(); //Div