Search code examples
piranha-cms

Piranha CMS: how to find child pages for a page


How can I find child page-item(s), to get its title and link from within a Block? (the way pages are structured in the manager)

I tried a bit with the Sitemap, but I'm having a little trouble instantiating the object. How do I do it?

It would be a bit of a hassle to loop through all the nodes in the Sitemap to find the correct page. Is there a better way?


Solution

  • To get the sitemap structure you simply call:

    var sitemap = api.Sites.GetSitemap();
    

    If you have multiple sites you'll need to specify the site you want, otherwise the sitemap for the default site is returned.

    var sitemap = api.Sites.GetSitemap(siteId);
    

    Once you have the sitemap you can get a partial sitemap from your current page and do something fun with the subpages with the following code:

    var sitemap = api.Sites.GetSitemap();
    var partial = sitemap.GetPartial(myPage.Id);
    
    foreach (var subpage in partial)
    {
        // Do your stuff here!
    }
    

    Best regards

    Håkan