1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Extracting a hierarchical list of pages using Fluent API

Extracting a hierarchical list of pages using Fluent API

4 posts, 0 answered
  1. Jacques
    Jacques avatar
    427 posts
    Registered:
    28 Jun 2007
    13 Feb 2013
    Link to this post
    When using the Fluent API I seem to get a list of page nodes without any form of hierarchy.

    So I started by trying to get a count of pages where the parent ID is null:
    App.WorkWith().Pages().LocatedIn(locatedIn).Where(p => p.ParentId == Guid.Empty).Count(out count);

    The count is always zero. I've tried p.ParentId == null too, but that doesn't work either.

    I have a method called _PageHierarchy(parentNode, locatedIn), which I call and within itself it's supposed to loop through pages with no parent ID then call the method again for child pages.
    Here's the method:

    private List<MyPageNode> _PageHiearchy(MyPageNode parentNode, PageLocation locatedIn)
            {
                List<MyPageNode> myPageNodes = new List<MyPageNode>();
                int count = 0;
     
                //if we're starting the list
                if (parentNode == null)
                {
                    //get count
                    App.WorkWith().Pages().LocatedIn(locatedIn).Where(p => p.ParentId == Guid.Empty).Count(out count);
                    App.WorkWith().Pages().LocatedIn(locatedIn).Where(p => p.ParentId == null).ForEach(p =>
                        {
                            //new node
                            MyPageNode newPageNode = new MyPageNode(p);
                            myPageNodes.Add(newPageNode);
     
                            //if this node has child nodes
                            App.WorkWith().Pages().LocatedIn(locatedIn).ThatBelongTo(p.Id).Count(out count);
                            if (count > 0)
                            {
                                newPageNode.ChildNodes.AddRange(_PageHiearchy(newPageNode, locatedIn));
                            }
                        });
                }
                else
                {
                    App.WorkWith().Pages().LocatedIn(locatedIn).ThatBelongTo(parentNode.InnerNode.Id).ForEach(p =>
                        {
                            MyPageNode newChildNode = new MyPageNode(p);
     
                            //add this node to the page nodes
                            myPageNodes.Add(newChildNode);
                        });
                }
     
                return myPageNodes;
            }

    I'm using a custom page node class to store the child nodes:

    public class MyPageNode
        {
            private PageNode _PageNode;
            private List<MyPageNode> _ChildNodes;
            public MyPageNode(PageNode pageNode)
            {
                _PageNode = pageNode;
            }
            public List<MyPageNode> ChildNodes
            {
                get
                {
                    if (_ChildNodes == null)
                    {
                        _ChildNodes = new List<MyPageNode>();
                    }
                    return _ChildNodes;
                }
            }
            public PageNode InnerNode
            {
                get
                {
                    return _PageNode;
                }
            }
            public string Title
            {
                get
                {
                    return InnerNode.Title;
                }
            }
            public Guid PageId
            {
                get
                {
                    return InnerNode.Id;
                }
            }
            public Guid ParentPageId
            {
                get
                {
                    return InnerNode.ParentId;
                }
            }
        }

    So the question is, how does one extract a hierarchical list? Or, how do I extract the list of pages in the root and then loop through each of those to extract its child pages followed by its child pages and so on?

    Regards,
    Jacques
  2. Stanislav Velikov
    Stanislav Velikov avatar
    1113 posts
    Registered:
    23 Aug 2017
    18 Feb 2013
    Link to this post
    Hello,

    Thank you for contacting us.

    Top level pages have parent page so their parent is not null. Frontend pages have parent page "Pages" and backend pages Administration->BackendPages have parent "Sitefintiy".

    To retreive top level pages filter them by parent Pages and to order pages as they are ordered in the backend use the property "Ordinal".

    PageManager manager = PageManager.GetManager();
     
               //get the top level pages which are child pages of Pages prent page
               var getTopLevelPages = manager.GetPageNodes().Where(p => p.Parent.Title == "Pages").OrderBy(o => o.Ordinal);
     
               List<string> childPageTitles = new List<string>();
               foreach (var childPage in getTopLevelPages)
               {
                   childPageTitles.Add(childPage.Title.ToString());
               }


    Regards,
    Anna Burton
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Jacques
    Jacques avatar
    427 posts
    Registered:
    28 Jun 2007
    18 Feb 2013 in reply to Stanislav Velikov
    Link to this post

    Thanks Anna, I'll give this a try.

    Any reason why I wouldn't be receiving these replies via email? 

  4. Jacques
    Jacques avatar
    427 posts
    Registered:
    28 Jun 2007
    18 Feb 2013 in reply to Stanislav Velikov
    Link to this post
    Is your suggested based on the Fluent API?
4 posts, 0 answered