3

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?

karel
  • 5,489
  • 46
  • 45
  • 50
Bent R.
  • 33
  • 2

1 Answers1

4

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

Håkan Edling
  • 2,723
  • 1
  • 12
  • 15
  • Thanks, that got me closer to the goal but not to the finish line. From within the Block model, i'm having trouble finding the Page Id - tried to get the url-path, to find the Slug, through the Request object, but that's only returning something like "/page/" and not the url displayed in the browser. – Bent R. Feb 20 '19 at 09:04
  • Hi there! You can get the current url, before it was rewritten from the IApplicationService from the property Url. If you're accessing it from a view and have created your project from one of our templates this service is injected to the variable WebApp in _ViewImports, in this case you can get it with WebApp.Url – Håkan Edling Feb 20 '19 at 11:41