I was recently tasked with cleaning up our instance of Umbraco. What I ended up with was a list of document types and the number of times they are used on the site.
Being an amateur programmer (I'm actually the Front End Guy) I wondered if we could get this list dynamically using the system itself. My code below lists a basic hierarchy down to level 2.
To avoid having to manually get all of the sub children, how could extend this code to list the full site structure and then subsequently, the document type used.
Thanks in advance!
// Get root node:
var nodes = Umbraco.TypedContentAtRoot();
// Loop through to show the node chidlren:
<ul>
@foreach (var node in nodes)
{
var nodeChildren = node.Children.ToList();
<li>@node.Name - @node.DocumentTypeAlias</li>
foreach (var nodeChild in nodeChildren)
{
<ul>
<li>@nodeChild.Name - @nodeChild.DocumentTypeAlias</li>
</ul>
}
}
</ul>
Try to use this code, it will render all Umbraco nodes with document type aliases:
@{
// Get root node:
var nodes = Umbraco.TypedContentAtRoot();
// Loop through to show the node chidlren:
<ul>
@foreach (var node in nodes)
{
@Traverse(node)
}
</ul>
}
@*Helper method to travers through all descendants*@
@helper Traverse(IPublishedContent node)
{
@*Select visible children *@
var items = node.Children;
@*If any items are returned, render a list *@
if (items.Any())
{
<ul>
@foreach (var item in items)
{
<li>
<a href="@item.Url">@item.Name - @item.DocumentTypeAlias</a>
@*Run the traverse helper again *@
@Traverse(item)
</li>
}
</ul>
}
}
Full site hierarchy of all document types
I was recently tasked with cleaning up our instance of Umbraco. What I ended up with was a list of document types and the number of times they are used on the site.
Being an amateur programmer (I'm actually the Front End Guy) I wondered if we could get this list dynamically using the system itself. My code below lists a basic hierarchy down to level 2.
To avoid having to manually get all of the sub children, how could extend this code to list the full site structure and then subsequently, the document type used.
Thanks in advance!
Hi Elliott
Try to use this code, it will render all Umbraco nodes with document type aliases:
Thanks,
Alex
Thanks Alex, exactly what I was after.
You are welcome, Elliott, have a great weekend!
For anyone else who comes across this topic.
This post shows you how to get a list of all doc types used and how many times they are used.
https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/54740-Document-Types-usage-count
is working on a reply...