Media Library Paths

Media Library Paths

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Collections.ChildList
  • Sitecore.Context
  • Sitecore.Data.Items.Item
  • Sitecore.Data.Items.MediaItem

Media Library Paths C# Web Control Code

Sitecore.Context.SetActiveSite("shell");
Sitecore.Data.Items.Item item = Sitecore.Context.ContentDatabase.Items["/sitecore/media library"];
if (item == null) return;

Sitecore.Collections.ChildList children = new Sitecore.Collections.ChildList(item);
if (children == 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 space
output.Write("{0}", item.DisplayName + " Paths");
output.RenderEndTag(); //P

string path = string.Empty;
foreach (Sitecore.Data.Items.Item child in children)
{
    path = ((Sitecore.Data.Items.MediaItem)child).Path;
    output.Write("{0}&#58;&#160;{1}", child.Name + "  Path", "<b>" + path + "</b><br/>");
}
output.RenderEndTag(); //Div

Notes

  • The current site is normally determined by the HttpRequest module
  • The shell site defines a content database for Sitecore UI tools, which is usually the master database
  • Use the SetActiveSite() method with caution