Extracting a hierarchical list of pages using Fluent API
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);
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;
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());
Thanks Anna, I'll give this a try.
Any reason why I wouldn't be receiving these replies via email?
Is your suggested based on the Fluent API?