Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Elliott Brown 40 posts 210 karma points
    Jul 21, 2017 @ 10:02
    Elliott Brown
    0

    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!

    // 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>
    
  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jul 21, 2017 @ 10:09
    Alex Skrypnyk
    100

    Hi Elliott

    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>
        }
    }
    

    Thanks,

    Alex

  • Elliott Brown 40 posts 210 karma points
    Jul 21, 2017 @ 10:18
    Elliott Brown
    0

    Thanks Alex, exactly what I was after.

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jul 21, 2017 @ 10:26
    Alex Skrypnyk
    0

    You are welcome, Elliott, have a great weekend!

  • Elliott Brown 40 posts 210 karma points
    Jul 21, 2017 @ 10:42
    Elliott Brown
    0

    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

Please Sign in or register to post replies

Write your reply to:

Draft