I'm in a child page for any site and would like to know the home node so I can retrieve some data from one of the properties in the home node but from a C# class.
Assuming you have a "HomePage" document type that your using and you have models builder creating models, then you can do something like:
currentPage.AncestorOrSelf<HomePage>()
where currentPage is your IPublishedContent based child page.
If you see anything online suggesting that you use the ContentService, don't! That would be massive overhead for what you want to do and it's more for creating and manipulating content than searching/browsing.
Hard to say not knowing what your class does and how it works, but probably your best bet then is to inject the IUmbracoContextFactory.
You can then get the Umbraco Context and query it for the homepage node, so to start you off, something like this might help.
using var umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext();
var umbracoContext = umbracoContextReference.UmbracoContext;
var homePage = umbracoContext.PublishedRequest?.PublishedContent.AncestorOrSelf<Homepage>();
Thanks for that. Unfortunately the value come back as null. If it helps publishedRequest is null however putting the same code inside a controller seems to find the node but not in a custom class.
If I'm on a page 4-5 levels down for any site (1,2 or 3), all I would like to do is get the home node for the site I'm currently on.
Once I have the home node I believe I can retrieve a value from one of the properties but it's trying to get to the home node where I'm having issues.
I have a standard C# class injected in IumbracoContextFactory. Created a method called Test and added in the code you posted above.
I then call this class from another part of the Umbraco system (which has already been built and working) but this new method doesn't return the home page.
Not sure if it helps, I injected a couple of other Umbraco services which allowed me to get to GetAtRoot() which is half way there.
The problem with GetAtRoot is it returns a list of home nodes so I can't select the node for the site I'm currently on.
I decided to mention this in case it helps but hopefully it doesn't distract your thoughts 😀
The place where you're calling this class from, what does the published request look like there?
Perhaps you can get it there and then pass it to your class as an IPublishedContent parameter or attribute? You should have access to the ancestors then in your class if you use what you've passed through?
That is another custom class which has other Umbraco services injected. There are a few methods which take in an IPublishedContent but all of those are having some value passed in.
I was attempting to do the same but struggling to get the home node where the others were getting data based on document types.
Get the home node in current site
Hi
I'm using Umbraco 13. My structure is
I'm in a child page for any site and would like to know the home node so I can retrieve some data from one of the properties in the home node but from a C# class.
How could I do that?
Assuming you have a "HomePage" document type that your using and you have models builder creating models, then you can do something like:
where currentPage is your IPublishedContent based child page.
If you see anything online suggesting that you use the ContentService, don't! That would be massive overhead for what you want to do and it's more for creating and manipulating content than searching/browsing.
Hi
I don't have currentPage. Which interface could I use to inject this?
Hi Jimmy,
Are you trying to do this in Razor, a RenderController, SurfaceController, custom api, etc?
Will affect how you can access it.
It's a custom class.
Thanks
Hard to say not knowing what your class does and how it works, but probably your best bet then is to inject the IUmbracoContextFactory.
You can then get the Umbraco Context and query it for the homepage node, so to start you off, something like this might help.
Thanks for that. Unfortunately the value come back as null. If it helps publishedRequest is null however putting the same code inside a controller seems to find the node but not in a custom class.
If I'm on a page 4-5 levels down for any site (1,2 or 3), all I would like to do is get the home node for the site I'm currently on.
Once I have the home node I believe I can retrieve a value from one of the properties but it's trying to get to the home node where I'm having issues.
So what does your custom class do Jimmy? How does it interact with the rest of your code? Is it an object, service, extension, etc.?
At the moment it doesn't do a lot.
I have a standard C# class injected in IumbracoContextFactory. Created a method called Test and added in the code you posted above.
I then call this class from another part of the Umbraco system (which has already been built and working) but this new method doesn't return the home page.
Not sure if it helps, I injected a couple of other Umbraco services which allowed me to get to GetAtRoot() which is half way there.
The problem with GetAtRoot is it returns a list of home nodes so I can't select the node for the site I'm currently on.
I decided to mention this in case it helps but hopefully it doesn't distract your thoughts 😀
Thanks
The place where you're calling this class from, what does the published request look like there?
Perhaps you can get it there and then pass it to your class as an IPublishedContent parameter or attribute? You should have access to the ancestors then in your class if you use what you've passed through?
That is another custom class which has other Umbraco services injected. There are a few methods which take in an IPublishedContent but all of those are having some value passed in.
I was attempting to do the same but struggling to get the home node where the others were getting data based on document types.
Do you want to post some of your code up on here? Might help us understand a bit more what your aiming for.
is working on a reply...