Copied to clipboard

Flag this post as spam?

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


  • Preetee Gangoosirdar 56 posts 257 karma points
    Mar 30, 2017 @ 11:54
    Preetee Gangoosirdar
    0

    How to get the parent content Node's name according to it's specific children doctype type

    Hi everyone,

    Need some help to query the parent Node name from the content structure for the following sections :Blog post , music band and events sections which are rendered in the same template via their respective Macro scripts

    Please find structure

    - BlogPostContainer with doctype alias container

        some children nodes with doctype alias *post*
    

    -MusicBandContainer with doctype alias container

        some children nodes with doctype alias *musicband*
    

    -EventContainer with doctype alias container

        some children nodes with doctype alias *event*
    

    var container =  Model.Content.Descendants().FirstOrDefault(x=>x.DocumentTypeAlias=="container");
    var containerName = container.HasProperty("containerTitle") && container.HasValue("containerTitle")? container.GetPropertyValue<string>("containerTitle") : container.Name. ;
    

    //this return me the first container Name which is BlogPost in the razor script for blog list since this was the first container to be created in the content structure

    For the other two section music band and event, i want to get their respective parent name also according to their specific children doctype

      @Model.Content.ChildrenAsList.Where("musicband").Parent.Name
    

    //not working error

    Thank you for kind response and help.

  • Sven Geusens 169 posts 881 karma points c-trib
    Mar 31, 2017 @ 12:58
    Sven Geusens
    0

    Simply put @Model.Content.ChildrenAsList.Where("musicband").Parent.Name doesnt work because ChildrenAsList returns a List

    .Parent is a property available on IPublishedContent, so you will have to do a foreach on the collection.

    Another issue is that you are using Descendants for a container/area kind of thing. Do you have containers inside containers ? If so, this can be the right.

    So going out from the following structure.

    • Node where you run this code on
      • Container A
        • Item A1
        • Item A2
        • Item AX
      • Container B
        • Item B1
        • Item B2
        • Item BX
      • Container C
        • Item C1
        • Item C2
        • Item CX
    • Another node

    I would do:

    //when you pass a string into the childrin function on an IPublishedContant, you filter on that documentTypeAlias
        foreach( var container in Model.Content.Children("container")
        {
            var containerName = container.HasProperty("containerTitle") && container.HasValue("containerTitle")? container.GetPropertyValue<string>("containerTitle") : container.Name. ;
        }
    
  • Preetee Gangoosirdar 56 posts 257 karma points
    Apr 01, 2017 @ 14:23
    Preetee Gangoosirdar
    100

    Hi Sven,

    Thanks you very much for kind help.

    Indeed this will return me a list of content nodes with the document type alias container . Indeed i dont need to use descendant as i wont have a container within another container.

    Since i need to display each section's name in the same template, i finally got the following 2 possible solutions as shown below ,

    Here is the content structure below :-

    Under EN content structure i have the following

    • EN Node

      • Container A (doctype alias container)

        • List item A1 (doctype alias musicband)
        • List item A2 (doctype alias musicband)
        • List item AX (doctype alias musicband)
      • Container B(doctype alias container)

        • List item B1 (doctype alias event)
        • List item B2 (doctype alias event)
        • List item BX (doctype alias event)
      • Container C(doctype alias container)

        • List item C1 (doctype alias post)
        • List item C2 (doctype alias post)
        • List item CX (doctype alias post)

    1.First solution : - assuming the each section name remains the same in my multi language website

    List all container that contains the content name music band

    var containerList = Model.Content.Children("container") .Where(x => x.Name.ToString().Contains("Music Band") );
    

    Display the section name of music band

    <h2>
    @foreach(var container in containerList){
    var containerName = container.HasProperty("containerTitle") && container.HasValue("containerTitle")? container.GetPropertyValue<string>("containerTitle") : container.Name;
    
          @Html.Raw(containerName)
    }
    </h2>
    

    Another solution that i got is

    to List all musicband nodes then check if parent doctype alias is container then break out the loop

    foreach(var container in Model.Content.Descendants().Where(x=>x.DocumentTypeAlias=="musicband")){
                     if(container.Parent.DocumentTypeAlias=="container"){
    
                         var containerName = container.Parent.HasProperty("containerTitle") && container.Parent.HasValue("containerTitle")? container.Parent.GetPropertyValue<string>("containerTitle") : container.Parent.Name;
                        <h2>                                    
                          @Html.Raw(containerName)
                       </h2>
                           break;
                    }
             }
    

    Again thanks for your time and clean out my problem. Have a great weekend.

Please Sign in or register to post replies

Write your reply to:

Draft