Search code examples
c#asp.net.netc1-cms

How to find out the parent page Id from IPageStructure? (Composite C1)


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.


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;