Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • schlubadub 102 posts 617 karma points
    Apr 02, 2015 @ 05:45
    schlubadub
    0

    Access CurrentPage.Site() from Webforms

    I am converting some Razor / MVC code to Webforms and I'm just trying to figure out what the equivalent of CurrentPage.Site() is?  [Using Umbraco 7.2.4]

    E.g. If I start with this:

    @{ var home = CurrentPage.Site(); }
    
    @if (home.Children.Any())
    {
       @* do stuff *@
    }

    I can do the same thing in Webforms by referencing the content of the home node - but I've had to hard-code the node id, which is obviously not ideal if I roll this out to other sites:

    var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    
    var home = umbracoHelper.Content(1069);
    
    if (home.Children.Any())
    {
       // do stuff
    }

    Can anyone tell me how to do this (get rid of Content(1069))? I thought using ContentAtRoot() might help but it returns a different type of data.

    Bear in mind that the page calling it will NOT be published in Umbraco anywhere (so "CurrentPage" may not make sense?), as it's all in Webforms while the main site is MVC.

  • schlubadub 102 posts 617 karma points
    Apr 02, 2015 @ 10:02
    schlubadub
    100

    After a LOT of aggravation and wasted time I finally managed to extra the node id using this:

     int rootNodeID = umbracoHelper.ContentAtRoot().FirstOrDefault().Id

    So putting it all together it's this:

    var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    
    int rootNodeId = umbracoHelper.ContentAtRoot().FirstOrDefault().Id;
    var home = umbracoHelper.Content(rootNodeId);

    if (home.Children.Any()) { 
    // do stuff
    }

    I haven't tested this with multiple sites, so hopefully it doesn't fall apart there!

  • schlubadub 102 posts 617 karma points
    May 26, 2015 @ 07:11
    schlubadub
    0

    UPDATE: Don't use the code I provided above in a multi-site arrangement as it does not work!!! It will return the same Home NodeID instead of the Home of the current site

    Use this: https://our.umbraco.org/forum/developers/api-questions/65075-Root-Home-Node-incorrect-in-Multi-Site-Setup

Please Sign in or register to post replies

Write your reply to:

Draft