Copied to clipboard

Flag this post as spam?

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


  • Koen van Ras 56 posts 361 karma points c-trib
    May 02, 2019 @ 13:41
    Koen van Ras
    0

    Get all nodes that contain a hostname

    Hi,

    I'm creating a custom section in Umbraco 8 to display properties for certain nodes. Now I need to load all the nodes to display them in a tree overriding the GetTreeNodes method in the TreeController. I figured out that I can do this using umbraco.cms.businesslogic.web.Domain.GetDomains() in older Umbraco versions. But how do I do this in Umbraco 8?

    I want a TreeNodeCollection where I add all the nodes that contain a hostname as a TreeNode.

    What I have to add the TreeNodes:

    var childNodes = new TreeNodeCollection();
    var domain = CreateTreeNode("1", "0", queryStrings, "Website", "icon-home", false);
    childNodes.Add(domain);
    return childNodes;
    
  • Corné Strijkert 80 posts 456 karma points c-trib
    May 02, 2019 @ 14:12
    Corné Strijkert
    100

    I want a TreeNodeCollection where I add all the nodes that contain a hostname as a TreeNode.

    Something like this?

    protected override TreeNodeCollection GetTreeNodes(string id, FormDataCollection queryStrings)
    {
        var contentService = Services.ContentService;
        var treeNodeCollection = new TreeNodeCollection();
    
        var domains = Services.DomainService.GetAll(false);
        foreach (var domain in domains)
        {
            if (domain.RootContentId != null)
            {
                var rootContent = contentService.GetById(domain.RootContentId.Value);
                if (rootContent != null)
                {
                    treeNodeCollection.Add(CreateTreeNode("1", "0", queryStrings, rootContent.Name, "icon-home", false));
                }
            }
        }
    
        return treeNodeCollection;
    }
    

    This code first get all available domains. The for each domain the root content node for that domain is added to the TreeCollection list.

  • Koen van Ras 56 posts 361 karma points c-trib
    May 02, 2019 @ 14:21
    Koen van Ras
    0

    Thats it! Thanks alot!

  • Koen van Ras 56 posts 361 karma points c-trib
    May 03, 2019 @ 07:46
    Koen van Ras
    0

    One more quick question, I created a root node where I give a value that it may contain children. After that I add another root node, but if I later add a child node where I give the first root node id as parent. The child node will show under both the root nodes. How can I specify under which parent the children should show up?

    protected override TreeNodeCollection GetTreeNodes(string id, FormDataCollection queryStrings)
    {
        if (id == "-1")
        {
            var rootNodes = new TreeNodeCollection();
    
            var domainSettings = CreateTreeNode("0", "-1", queryStrings, "Domain Settings", "icon-settings", true);
            rootNodes.Add(domainSettings);
    
            domainSettings = CreateTreeNode("1", "-1", queryStrings, "Test", "icon-settings", true);
            rootNodes.Add(domainSettings);
    
            return rootNodes;
        }
        else
        {
            var contentService = Services.ContentService;
            var treeNodeCollection = new TreeNodeCollection();
            var domains = Services.DomainService.GetAll(false);
    
            foreach (var d in domains)
            {
                if (d.RootContentId != null)
                {
                    var rootContent = contentService.GetById(d.RootContentId.Value);
                    if (rootContent != null)
                    {
                        treeNodeCollection.Add(CreateTreeNode(rootContent.Id.ToString(), "0", queryStrings, rootContent.Name, "icon-home", false));
                    }
                }
            }
    
            return treeNodeCollection;
        }
    
        throw new NotSupportedException();
    }
    
  • Corné Strijkert 80 posts 456 karma points c-trib
    May 03, 2019 @ 08:02
    Corné Strijkert
    0

    The child node will show under both the root nodes. How can I specify under which parent the children should show up?

    The tree is lazy loaded, so for each node you expand the method GetTreeNodes() will be called with the current node id as parameter.

    For the root level you are correctly checking if the id is equal to -1, but when the id is not -1 you always return the same tree collection. So, in the else statement (or better transform your code to a switch statement or something similar) you should check the provided ID, like so in pseudocode:

    switch(id)
    {
        case "-1":
            // return Domain settings node with id 0
            // return Test ndoe with id 1
        case "0";
            // return nodes that are children of Domain settings
        case "1";
            // return nodes that are children of Test
    }
    
  • Koen van Ras 56 posts 361 karma points c-trib
    May 03, 2019 @ 08:16
    Koen van Ras
    1

    Awesome that did the trick! Thanks :)

Please Sign in or register to post replies

Write your reply to:

Draft