Developer forum

Forum » Development » A little API help - get title of root/first-parent page via API

A little API help - get title of root/first-parent page via API

Hans Ravnsfjall
Hans Ravnsfjall
Reply

Hi

I am trying to fetch the title of the first/root (level 0) parent of a page at any given time. I have tried doing this:

@Dynamicweb.Frontend.PageView.GetPageviewByPageID(<placeholder for parent pageid>)).Meta.Title

But this only gets me the title of the parent just above the page I am on. The root parent i want to fetch, can be 3 levels above the current page. Any guidance on how I can fetch this via the API? Can´t find any relevant method or value in the PageView Class. Maybe I am missing something?

And can i fetch it in one go, or do I have to iterate backwards/upwards untill I reach the Root?

 

/Hans


Replies

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Hans,

 

You should be able to do Model.TopPage.ID and that would give you the root page of that hierarchy. From there you don't need to get the pageview (because that will load far too much information for what you need, so you can simply do

var topPageTitle = Dynamicweb.Services.Pages.GetPage(Model.TopPage.ID).MetaTitle;

 

Best Regards,

Nuno Aguiar

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Thank you Nuno

that works if you are on a single page - but in this case,  I am listing a number of pages in the QueryPublisher, and in that query - I don´t know how to get the specific TopPage.ID for each of those pages.

/Hans

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Hans,

 

Looking at the Repository, I can see the Content Builder does not index the TopPage Id. In any case you should be able to do this

 

@foreach (var result in GetLoop("QueryResultItem"))
{
    var page = Dynamicweb.Services.Pages.GetPage(result.GetInteger("PageId"))
    var topPageTitle = Dynamicweb.Services.Pages.GetPage(page.TopPage.ID).MetaTitle;
            
    // YOUR CODE HERE
}

 

Beware of performance though.

 

Nuno

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Thanx Nuno, really appreciate your reply 🙏

But the Pages class does not have a TopPage property, so this won´t work either, right?

/Hans

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply
This post has been marked as an answer

Hi Hans,

 

Upps, you're right I jumped at that because we usually get the same properties in the Page object. You'll have to probably work you're way up by creating a method like GetTopPage(int pageId)

 

private static Page GetTopPage(int pageId) {

    var page = Dynamicweb.Services.Pages.GetPage(pageId)

    if (page == null || page.level == 0) return null;

    if (page.level == 1) return page;

    return GetTopPage(page.Parent.ID);

}

 

then you can do 

@foreach (var result in GetLoop("QueryResultItem"))
{
    var topPage = GetTopPage(result.GetInteger("PageId"))
    var topPageTitle = topPage.MetaTitle;
            
    // YOUR CODE HERE
}

 

This might get you there. I typed this here, so beware for typos and other funny stuff.

 

Best Regards,

Nuno Aguiar

Votes for this answer: 1
 
Jeppe Eriksson Agger Dynamicweb Employee
Jeppe Eriksson Agger
Reply
This post has been marked as an answer

Hi all,

I know I'm a little late to the party, but I would just suggest that you make this calculation when you build the index instead of having to recalculate it on every request in the frontend. It is fine to have a little performance degradation when building the index, but it isn't recommended to do that in the frontend.

You can easily do this by extending the ContentIndexBuilder -- assuming you're using the ContentIndexBuilder -- and adding a new field that contains the value. Take a look at this tutorial from the T3 course: https://doc.dynamicweb.com/training/t3-platform-developer/t3-platform-developer/3-5-extending-indexing.

- Jeppe

Votes for this answer: 1
 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

You're absolutely right Jeppe ... that's much better. I walked into that "trap" :P

 

You must be logged in to post in the forum