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.
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!
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
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:
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:
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.
After a LOT of aggravation and wasted time I finally managed to extra the node id using this:
So putting it all together it's this:
I haven't tested this with multiple sites, so hopefully it doesn't fall apart there!
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
is working on a reply...