Question

Is this the best way to get the parentId of a page from the IPageStructure in Composite C1 within a UserControl ?

string pPageId = SitemapNavigator.CurrentPageId.ToString();
Guid parentId = connection.Get<IPage>().Where(p => p.Id == new
   Guid(pPageId)).First().GetParentId();

I do use the present pageId as a string, which helps in the above case seeing as .First() can't be used on Guid.

Was it helpful?

Solution

Take a look at the SitemapNavigator instance class you can grab from DataConnection - when you get the data bound instance it gives you access to the current page in form of a PageNode class from which you can access parent, child pages. You also have access to titles, descriptions etc.

// General (verbose) example - getting parent page ID
using (var connection = new DataConnection())
{
    var currentPageNode = connection.SitemapNavigator.CurrentPageNode;
    var parentPageNode = currentPageNode.ParentPage;
    var parentPageId = parentPageNode.Id;
} 

If you are doing this from a Razor Function there is already a data connection available (this.Data) so the above can be written like this:

// Razor example - getting parent page ID    
var parentPageId = Data.SitemapNavigator.CurrentPageNode.ParentPage.Id;

OTHER TIPS

One option is just to rely 100% on the standard asp.net way using the SiteMapProvider functionality.

Get a specific node like this: var node = SiteMap.CurrentProvider.FindSiteMapNodeByKey("..."). Get the Parent node like this: var parentNode = node.ParentNode. Get its id like this: var id = parentNode.Key

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top