Skip to content

Instantly share code, notes, and snippets.

@KevinJump
Last active February 19, 2020 16:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save KevinJump/df42f978ac0518147b25 to your computer and use it in GitHub Desktop.
Save KevinJump/df42f978ac0518147b25 to your computer and use it in GitHub Desktop.
Umbraco Macro file to render a list of documents on a page
@inherits Umbraco.Web.Macros.PartialViewMacroPage
@using Umbraco.Web.Models
@{
var folders = Model.GetParameterValue<string>("mediaFolder", "")
.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
var subFolders = Model.GetParameterValue<bool>("subFolders", true);
var selection = Umbraco.TypedMedia(folders).Where(x => x != null);
}
@if (selection.Any())
{
int n = 0;
<div class="media-library" id="media-library">
@foreach (var mediaNode in selection)
{
n++;
<div class="media-listing">
@RenderMediaFolder(mediaNode, n, subFolders)
</div>
}
</div>
}
@helper RenderMediaFolder(IPublishedContent folder, int count, bool subFolders)
{
if ( count > 1 )
{
<div class="page-header">
<a data-toggle="collapse" data-parent="#media-library" href="#listing-@count" aria-expanded="true" aria-controls="listing-@count">
<h3>@folder.GetPropertyValue("title", folder.Name)</h3>
</a>
</div>
}
var subCount = 0;
<div id="listing-@count" class="collapse @Umbraco.If(count==1, "in", "")">
<ul class=" media-item-list list-unstyled">
@foreach (var item in folder.Children.Where(x => x.IsVisible()))
{
var itemUrl = item.GetPropertyValue<string>("umbracoFile");
var itemName = item.GetPropertyValue<string>("title", item.Name);
var itemType = "unknown";
if (!string.IsNullOrWhiteSpace(itemUrl))
{
itemType = System.IO.Path.GetExtension(itemUrl).Trim('.');
}
if (item.DocumentTypeAlias == "Folder")
{
if ( subFolders )
{
subCount = subCount + 1;
<!-- child folder -->
<li class="media-item-folder">
@RenderMediaFolder(item, count+100+subCount, subFolders)
</li>
}
}
else
{
<li class="media-item-item media-@itemType">
<a href="@itemUrl" target="_blank">
<h4>
@itemName <small><span class="glyphicon glyphicon-file" aria-hidden="true"></span></small></h4>
</a>
<p>@item.GetPropertyValue("summary")</p>
</li>
}
}
</ul>
</div>
}
@KevinJump
Copy link
Author

The macro for this has two parameters -

  • mediaFolder a media picker
  • SubFolders a true/false

The macro will render anything that is in the mediaFolder , sub folders is if you want to also list documents in sub folders (you can just take this out and render the folder)

This assumes you're documents have a Title and Summary, which works well if you want Examine to find the pages and documents.

The resultant HTML is a bootstrap Accordion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment