Copied to clipboard

Flag this post as spam?

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


  • Paul Griffiths 370 posts 1021 karma points
    Mar 04, 2015 @ 21:02
    Paul Griffiths
    0

    NULLListing all child nodes of a certain document type

    Hi all,

    Realy hoping someone can help me with this one i have been struggling for a few days now.

    I have the following tree structure in my umbraco v6.1.2 site

    enter image description here

    and i would like to display all the 3rd level node names in a dropdown list which is part of my c# user control. I am able to target a certain node and display its children in a dropdown using the following code (which i used for another dropdown to bind the driver names)

    //If you need a specific node based on ID use this method (where 123 = the desired node id)
                Node driverParentNode = new Node(1083);
    
                //To get the children of the driverParentNode as a List collection and sort the nodes alpha
                var driverChildNodes = driverParentNode.ChildrenAsList.OfType<Node>().OrderBy(n => n.Name);
    
                //Create a new list to store the child nodes
                List<string> finalDriverList = new List<string>();
    
                //Iterating over nodes driver child nodes collection and add them to the final driver list
                foreach (var node in driverChildNodes)
                {
                    finalDriverList.Add(node.Name);
    
                }
    
                //display the final driver list in the dropdown control    
                ddlPreferedDriver.DataSource = finalDriverList;
                ddlPreferedDriver.DataBind();
                ddlPreferedDriver.Items.Insert(0, new ListItem("Select Driver...", "0"));
    

    Using the same code i can get all the level two items into a dropdown list (mercedes,ford etc.) into the vehicle ddl but how can i get the children of these into the ddl?

    Now i thought i could achieve this by targeting the VehicleManufacturer doc type(id=1184) which is what all second level nodes are created from. Unfortunately my efforts so far have failed and im unsure if this can even be achieved.

    List<Node> foundNodes = new List<Node>();
    DocumentType nodeId = new DocumentType(1184);
            foreach (Node childNode in nodeId.Children)
            {
                var child = childNode;
                if (child.NodeTypeAlias == "VehicleProfile")
                {
                    foundNodes.Add(child);
                }
            }
    
            //display the final driver list in the dropdown control    
            ddlPreferedVehicle.DataSource = foundNodes;
            ddlPreferedVehicle.DataBind();
            ddlPreferedVehicle.Items.Insert(0, new ListItem("Select Vehicle...", "0"));
    

    If someone out there has any info which may help me achieve this then i would be really gratefull

    cheers all

    Paul

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Mar 05, 2015 @ 01:07
    Alex Skrypnyk
    0

    Hi Pual,

    When you are trying to get all child nodes by DocumentType.Children you are on the wrong way. You can't do that.

    Try to look at this code example :

    string docTypeAlias ="VehicleProfile";
    DocumentType dt =DocumentType.GetByAlias(docTypeAlias);
    Document.GetDocumentsOfDocumentType(dt.Id);

    Or loot at this link http://blog.codelab.co.nz/2013/01/13/get-all-nodes-by-documenttype/

     

    Thanks,

    Alexander

Please Sign in or register to post replies

Write your reply to:

Draft