Get Media Folders

Get Media Folders

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Database
  • Sitecore.Data.Items.Item
  • Sitecore.StringUtil
  • Sitecore.TemplateIDs
  • System.String

Media Folders C# Web Control Code

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

string query = System.String.Format("//*[@@templateid='{0}']", Sitecore.TemplateIDs.MediaFolder);

Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item[] descendants = db.SelectItems(query);
if (descendants == 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);
string displayName = Sitecore.StringUtil.GetString(item.DisplayName, item.Name);
//using : for colon and   for space
output.Write("{0}: {1}", "Item", displayName);
output.RenderEndTag(); //P

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("{0}", "Displaying Ten Filtered Descendants");

int i = 0;
foreach (Sitecore.Data.Items.Item descendant in descendants)
{
    i++;
    output.Write("{0}&#58;&#160;{1}", "Media Folder", "<b>" + descendant.DisplayName + "</b><br/>");
    if (i >= 10) break;
}

output.RenderEndTag(); //Div


Media Folders C# Web Control Output

Displaying Ten Filtered Descendants

Media Folder: Files
Media Folder: MP4
Media Folder: TIC-Docs
Media Folder: CodeSnippets
Media Folder: Crosswords
Media Folder: WMV
Media Folder: Images
Media Folder: Awards
Media Folder: Color
Media Folder: Grayscale