I want to build an ASP.NET usercontrol that shows a homepage navigation including only pages of a certain page type (which I named ContentPage).
In the database table for published pages this information is present for each page (Column PageTypeID
) but I am unable to find any way to access it via the Composite API from my code.
using(var conn = new DataConnection())
{
var siteMapNav = new SitemapNavigator(conn);
var pages = siteMapNav.CurrentHomePageNode.GetPageNodes(SitemapScope.DescendantsAndCurrent);
// what now?
}
How can I access the PageType information for a page from C#?
You can use the data connection to look up the page type on IPage.PageTypeId and then combine the the result from SitemapNavigator:
using (var conn = new DataConnection())
{
Guid pageTypeIdInQuestion = Guid.Empty; // put type id here
var siteMapNav = new SitemapNavigator(conn);
var pagesOfDesiredType = conn.Get<IPage>().Where(f => f.PageTypeId == pageTypeIdInQuestion).Select(f => f.Id);
var pages = siteMapNav.CurrentHomePageNode.GetPageNodes(SitemapScope.DescendantsAndCurrent).Where(f=> pagesOfDesiredType.Contains(f.Id));
}
Consider caching the result you get here and flush it with add/update/delete events on IPage and IPageStructure.