+1-888-365-2779
Try Now
More in this section

Forums / 3.x Pre-release forums (retired) / API Question: Finding pages based upon a parent page

API Question: Finding pages based upon a parent page

20 posts, 0 answered
  1. fregas baratis
    fregas baratis avatar
    185 posts
    Registered:
    14 Nov 2002
    20 Jan 2007
    Link to this post
    I have a situation where programatically, i need to know all the direct child pages of a given page.  So if I have a page called news, and it has 3 pages under it, how do i get a collection of sitefinity page objects?  This was pretty easy under sitefinity 2.7 but i'm not sure wehre to start in 3.0.  Do i need to do something with the Nolics o/r mapper to get this data?

    Thanks,
    Craig
  2. Sean
    Sean avatar
    271 posts
    Registered:
    31 May 2006
    21 Jan 2007
    Link to this post
    Hi Craig,

    I had to do the same thing, with a little help from Reflector and some sleuthing I found some code that worked, I am not sure if it is the best way to do it but I am sure Telerik will let you know...

    add:

    using Telerik.Cms;
    using Telerik.Cms.Web;

    now in whatever function you want to use...

    object pageId = HttpContext.Current.Items["cmspageid"];
    if (pageId != null)
    {
    CmsPage cmsPage = new CmsManager().GetPage((Guid)pageId);
    if (cmsPage != null)
    {
        cmsPage.ChildPages //child pages collection
    }
    }

    This works for me to get the ChildPages collection from there you should be able to figure out what you need to..  This works with Beta 3 but Telerik will no doubt let you know the proper implementation.

    Cheers
    Sean

  3. Rebecca
    Rebecca avatar
    536 posts
    Registered:
    24 Sep 2012
    22 Jan 2007
    Link to this post
    Hi Sean,

    Hello Fregas,

    There are several ways you could do this.

    One way is to use System.Web.SiteMap.CurrentNode.ChildNodes collection. SiteMap should be sufficient if you need general information such as URLs and Titles. For Sitefinity specific information, nodes in the SiteMap hierarchy can be cast to Telerik.Cms.CmsSiteMapNode which keeps reference to the corresponding Telerik.Cms.CmsPage (the Sitefinity’s page object). Unfortunately, this reference is not publicly exposed. I just created a public property and you will be able to use it with the next update.

    Another approach would be to use Telerik.Cms.CmsManger but I do not suggest this as CmsManger is still subject to change.

    You could also use the Page property of your control to obtain reference to CmsPage but I don’t recommend this approach either as there are some considerations that must be taken into account. There will be a help topic on this subject.

    Sincerely yours,
    Bob
    the telerik team 

  4. Sean
    Sean avatar
    271 posts
    Registered:
    31 May 2006
    22 Jan 2007
    Link to this post
    Hi Bob,

    Thanks for that, yeah I had a feeling that you might say the CmsManager might change in the future :)  so I am glad that there is going to be a way to access the CmsPage object through the Sitemap, that certainly seems better that how I was doing it in terms of version changes and the like.

    Thanks
    Sean
  5. fregas baratis
    fregas baratis avatar
    185 posts
    Registered:
    14 Nov 2002
    22 Jan 2007
    Link to this post
    My issue is, i need to know the child pages of another page, not the one i'm currently on.  Its sounds like i'll have to use the CmsPage manager for this, even though I may have to fix it later...
  6. fregas baratis
    fregas baratis avatar
    185 posts
    Registered:
    14 Nov 2002
    22 Jan 2007
    Link to this post
    Also, is there a way to do this by page name such as News.aspx?

    Craig
  7. Bob
    Bob avatar
    330 posts
    Registered:
    24 Sep 2012
    23 Jan 2007
    Link to this post
    Hello Fregas,

    The best approach would be:

    SiteMapNode node = SiteMap.Provider.FindSiteMapNode("~/News.aspx");

    Greetings,
    Bob
    the telerik team
  8. fregas baratis
    fregas baratis avatar
    185 posts
    Registered:
    14 Nov 2002
    23 Jan 2007
    Link to this post
    Nice!  Way simpler than what i was doing...

    Thanks for your help.
  9. fregas baratis
    fregas baratis avatar
    185 posts
    Registered:
    14 Nov 2002
    25 Jan 2007
    Link to this post
    Well that didn't work.  See the two exceptions below.  The first error i get on the home page.  I can't seem to find a page by url, and for some reason it can't even find the current page. 

    Then I tried a test on another page, and it did find the current node, but the child nodes don't seem to be the right data source.



    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    Line 36:                     SiteMapNode node = SiteMap.CurrentNode;
    Line 37:                     
    Line 38:                     this.SecondaryNav.DataSource = node.ChildNodes;
    


    Unable to cast object of type 'System.Web.UI.WebControls.ReadOnlyDataSource' to type 'System.Web.UI.IHierarchicalDataSource'.

    ------------------------------------------------------------------------------------------------

    Exception Details: System.InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.ReadOnlyDataSource' to type 'System.Web.UI.IHierarchicalDataSource'.

    Source Error:

    Line 38:                     this.SecondaryNav.DataSource = node.ChildNodes;
    Line 39:                     this.SecondaryNav.ItemDataBound += new Telerik.WebControls.RadMenuEventHandler(SecondaryNav_ItemDataBound);
    Line 40:                     this.SecondaryNav.DataBind();
    Line 41:                 }
    Line 42:             }

    Source File: c:\projects\DFWI\DFWI_Mainstreet v2\Development\Enilon.MainStreetArts.Web\App_Master\Default.master.cs    Line: 40

    Stack Trace:
  10. Bob
    Bob avatar
    330 posts
    Registered:
    24 Sep 2012
    26 Jan 2007
    Link to this post
    Hi Fregas,

    Please download and examine the sample project from this link http://www.sitefinity.com/support/forums/support-forum-thread/b1043S-gmckt.aspx

    There is third control demonstrating FindSiteMapNode method placed on the home page.


    Greetings,
    Bob
    the telerik team
  11. fregas baratis
    fregas baratis avatar
    185 posts
    Registered:
    14 Nov 2002
    30 Jan 2007
    Link to this post
    Thanks, I was able to get past that error using the ChildNodes.GetHierarchicalDataSourceView() to databind to the menus.  However, now i have another issue where if you tell a page not to show up in the navigation thru sitefinity, that page does not exist in the SiteMap at all.  This is a problem, as the Sitemap is then the main way you would need to find and display information about the hidden page, unless you use the Nolics data objects.

    Thanks for your help.

    Craig
  12. Bob
    Bob avatar
    330 posts
    Registered:
    24 Sep 2012
    31 Jan 2007
    Link to this post
    Hello Fregas,

    Add a second SiteMap provider like this:

    <siteMap defaultProvider="CmsSiteMapProvider" enabled="true">
        
    <providers>
            
    <clear/>
            
    <add name="CmsSiteMapProvider" description="Displays Cms Pages" type="Telerik.Cms.Web.CmsSiteMapProvider"/>
            
    <add name="AllPages" navigable="false" type="Telerik.Cms.Web.CmsSiteMapProvider"/>
        
    </providers>
    </
    siteMap>

    It is important to set the navigable attribute to false. This will indicate to the provider that it should return pages regardless of their settings for navigable. Then you can use it like this:

    SiteMap.Providers["AllPages"].RootNode.ChildNodes.GetHierarchicalDataSourceView();

    Sincerely yours,
    Bob
    the telerik team
  13. fregas baratis
    fregas baratis avatar
    185 posts
    Registered:
    14 Nov 2002
    31 Jan 2007
    Link to this post
    thanks again bob.  I'll try that.
  14. fregas baratis
    fregas baratis avatar
    185 posts
    Registered:
    14 Nov 2002
    31 Jan 2007
    Link to this post
    I tried that, and i AM able to see that the pages are in the collection thru the debugger, but they aren't rendering in the radmenu for some reason.  I even tried this using your SiteNavigationExample but with the same result.  I am databinding in the codebehind rather than in thru a <asp:SiteMapDataSource > object.
  15. Bob
    Bob avatar
    330 posts
    Registered:
    24 Sep 2012
    01 Feb 2007
    Link to this post
    Hi Fregas,

    Make sure you are binding using the correct provider. For example, if you are using SiteMapDataSource control with settings described in the previous post, you will need to set the SiteMapProvider attribute/property to “AllPages”. If the provider is not specified, the default one will be used.

    <asp:SiteMapDataSource ID="SiteMapDataSourc1" SiteMapProvider="AllPages" runat="server" />
    <radM:RadMenu ID="RadMenu1" DataSourceID="SiteMapDataSourc1" runat="server"></radM:RadMenu>

    I’m not sure if it is possible to bind RadMenu directly to hierarchical data source without using SitemapDataSouce control. An alternative would be to manually populate the menu by traversing the hierarchy.

    Best wishes,

    Bob
    the telerik team

  16. fregas baratis
    fregas baratis avatar
    185 posts
    Registered:
    14 Nov 2002
    13 Feb 2007
    Link to this post
    I tried traversing the hierarchy, but while each node has a title property which displays fine, they all have a blank URL property.
  17. fregas baratis
    fregas baratis avatar
    185 posts
    Registered:
    14 Nov 2002
    13 Feb 2007
    Link to this post
    Also, from a developer/API point of view, i really think the entire sitemap should be in in one provider/datasource, regardless of whether the pages are visible or not.
  18. Bob
    Bob avatar
    330 posts
    Registered:
    24 Sep 2012
    16 Feb 2007
    Link to this post

    Hi Fregas,

    Well it’s up to you how many data providers you will use. Of course you can set the default provider to return all pages but then you need to make your own implementation of the SiteMapDataSource control in order to prevent pages marked as non navigable from displaying on the menus.

    Another possibility is to use the SitefintiyManager like this:

    SitefinityManager manager = new SitefinityManager();
    IList rootPages = manager.GetPages(Guid.Empty);
    foreach (CmsPage page in rootPages)
    {
        
    IList children = page.ChildPages;
        
    //Do something or traverse the hierarchy to find the desired page
    }

    Sincerely yours,
    Bob
    the telerik team
  19. fregas baratis
    fregas baratis avatar
    185 posts
    Registered:
    14 Nov 2002
    16 Feb 2007
    Link to this post
    Yeah, i think thats what i'll have to do for now, and just avoid the whole sitemap thing entirely for this part of it.  But i think its a bug where the URLs on the sitemap come in blank if the pages are marked invisible.
  20. Bob
    Bob avatar
    330 posts
    Registered:
    24 Sep 2012
    19 Feb 2007
    Link to this post
    Hello Fregas,

    You are right this is a bug. It has already been fixed and will be available in the next update.

    Thanks,
    Bob
    the telerik team
Register for webinar
20 posts, 0 answered