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;
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();
}
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
}
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:
Something like this?
This code first get all available domains. The for each domain the root content node for that domain is added to the
TreeCollection
list.Thats it! Thanks alot!
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?
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:
Awesome that did the trick! Thanks :)
is working on a reply...