Copied to clipboard

Flag this post as spam?

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


  • Bhawna Jain 16 posts 96 karma points
    Aug 30, 2017 @ 06:26
    Bhawna Jain
    0

    Get list of distinct document type under a node.

    I have a document type named "home2" and it has various template allowed to used when creating page.how do i get unique list of DocumentType alias..I have written this code..which gets all children of "home2" and returns their doctype alias..but i want distinct list.

    Cause I may have two pages of same doctype:

    @{var node2 = umbraco.uQuery.GetNodesByType("home2");
         if (node2.Any())
         {IPublishedContent about = Umbraco.TypedContent(node2.First().Id);
          var selection = Umbraco.Content(about.Id);
          foreach(var items in selection.Children()){
              @items.DocumentTypeAlias()}}}
    

    I am really stuck in this one..please help

  • Laurence Gillian 600 posts 1219 karma points
    Aug 30, 2017 @ 08:07
    Laurence Gillian
    101

    Hi Bhawna,

    Having 1 document type with multiple templates can sometimes get a little fragile, you might find it easier if you have a 1 to 1 relationship between Document Types and Templates, giving you more structure within your content - which should make it easier to query nodes / separate concerns.

    Example:

    Doctype: home => Template: home
    Doctype: about => Template: about

    For your generic pages, you could perhaps then have an article document type too.

    Umbraco has a handy feature that allows you to change a document type by right clicking on the node in the content tree. This is super handy, as it means you can adjust and refine the structure as your are working.

    In response to your question, I've included some code below, that shows how to make the content queries in a slightly different and hopefully clearer way.

    Example:

    @{ var home = Model.Content.AncestorOrSelf("home"); }
    
    @* List all children *@
    @foreach (var item in home.Children())
    {
        <p>@item.Name, @item.DocumentTypeAlias</p>
    }
    

    If you had a separate doc type about you could select that node.

    @{ 
       var home = Model.Content.AncestorOrSelf("home");
       var about = home.Descendant("about");
     }
    

    You could then work with its children

    @foreach (var item in about.Children())
    {
        <p>@item.Name, @item.DocumentTypeAlias</p>
    }
    

    Or even filter the children again by doctype

    @foreach (var item in home.Children("articleWithImage"))
    {
        <p>@item.Name, @item.DocumentTypeAlias</p>
    }
    

    Hope that gives you some ideas and helps!

    Good luck, Laurie

  • Bhawna Jain 16 posts 96 karma points
    Sep 01, 2017 @ 06:18
    Bhawna Jain
    0

    Hi Laurence,

    Thank you so much for your answer with some well explained ways . But my question remains intact.

    what if i have multiple pages even of some general type like 'article' for instance. Is there any way to get exact number number of pages using 'article' doctype. ???

    Reason for this..in my case is ..i want to give all <div> in my page their IDs,so that i can scroll to that section.

  • Alex Skrypnyk 6150 posts 24110 karma points MVP 8x admin c-trib
    Aug 30, 2017 @ 10:32
    Alex Skrypnyk
    0

    Hello

    Also be sure that you are not using "uQuery.GetNodesByType" and "Descendant", it's really heavy methods, look how to avoid it here - https://our.umbraco.org/documentation/reference/Common-Pitfalls/

    Thanks

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft