Reputation: 663
I'm currently trying to create a custom tree and I'm running into trouble when trying to render a nodes children. After browsing various articles/posts I'm at this point:
public override void Render(ref XmlTree tree)
{
List<Node> articles = NodeUtil.GetAllNodesOfDocumentType(-1, "Promoter");
Node article = articles.Where(p => p.CreatorID == UmbracoEnsuredPage.CurrentUser.Id).FirstOrDefault();
if(promo != null)
{
var dNode = XmlTreeNode.Create(this);
dNode.NodeID = article.Id.ToString();
dNode.Text = article.Name;
dNode.Icon = "doc.gif";
dNode.Action = "javascript:openArticle(" + article.Id + ")";
dNode.Source = article.Children.Count > 0 ? this.GetTreeServiceUrl("" + article.Id) : "";
tree.Add(dNode);
}
}
The code above gets the article belonging to the current user (for the sake of testing, each user only has one article at the moment). I then attempt to print out the children of this article but instead of getting the desired output, I get the follwowing:
Article Name
- Article Name
- Article Name
- Article Name
Each time I expand a node, it just seems to render the same node, and goes on and on.
I've seen other ways of using the treeservice, like:
TreeService treeService = new TreeService(...);
node.Source = treeService.GetServiceUrl();
But I get an error saying there is no GetServiceUrl method that takes 0 arguments. I assume the method above was for earlier versions?
Upvotes: 2
Views: 1989
Reputation: 29474
It took me a while to work this out. Here is the solution, hope it would help someone.
const string PARENT_ID = "10"; // The ID of the node that has child nodes
public override void Render(ref XmlTree tree)
{
if (this.NodeKey == PARENT_ID) // Rendering the child nodes of the parent folder
{
// Render a child node
XmlTreeNode node = XmlTreeNode.Create(this);
node.NodeID = "11";
node.Text = "child";
node.Icon = "doc.gif";
node.Action = ...
tree.Add(node);
}
else // Default (Rendering the root)
{
// Render the parent folder
XmlTreeNode node = XmlTreeNode.Create(this);
node.NodeID = PARENT_ID;
node.Source = this.GetTreeServiceUrl(node.NodeID);
node.Text = "parent";
node.Icon = "folder.gif";
tree.Add(node);
}
}
Upvotes: 2
Reputation: 9051
The output suggests that the node tree you're building is nesting each child node - this is because the nodeId is being reset to -1 with each pass.
This post on our.umbraco.org describes the same problem, and suggests that you use NodeKey instead of ID to move between nodes.
**
Not necessarily helpful but I would use the uQuery language extensions that comes with the ucomponents package (and who installs Umbraco without ucomponents?), to simplify the method calls:
For example:
List<Node> articles = uQuery.getNodesByType("Promoter");
foreach(Node article in articles)
{
List<Node> children = article.GetDescendantNodes();
... build tree
}
Upvotes: 1