Try our conversational search powered by Generative AI!

Recursive Menu Building

Vote:
 

Hi, I'm trying to figure out why the bollowing snippet isn't behaving as expected. It should iterate through all content and child content, find any content that is marked "VisibleInMenu" and then add them to a list.

Func, IEnumerable> filter =
pages => pages.FilterForDisplay(requirePageTemplate, requireVisibleInMenu);
foreach (var menuItem in menuItems.ToList())
{
bool children = false;
bool.TryParse(menuItem.HasChildren.Value.ToString(), out children);
if (children)
{
var childItems = contentLoader.GetChildren(menuItem.Page.ContentLink)
.FilterForDisplay(requirePageTemplate, requireVisibleInMenu)
.Select(x => CreateMenuItem(x, currentContentLink, pagePath, contentLoader, filter))
.ToList();

childItems = recursiveMenu(childItems, pagePath, contentLoader, currentContentLink, requirePageTemplate, requireVisibleInMenu);

menuItems.AddRange(childItems);
}
}

return menuItems;



Any help would be much appreciated.  Thanks.

#145029
Feb 23, 2016 22:02
Vote:
 

Use Get descendants instead? Then you can filter that on access rights, visible in menu etc...?

Can't see anything obviously wrong. Do you have the contents of that recursive function? What does it actually return? Errors?

#145037
Feb 23, 2016 22:27
Vote:
 

I actually figured out what I've been doing wrong. I'm not precisely sure how to fix it but my logic in this snippet is working perfectly. It only gets added to the menu if its parent is part of the menu structure. If the parent is not part of the structure, neither is the child. In retrospect the problem was with my logic, not with the code itself.

#145039
Feb 24, 2016 0:38
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.