Copied to clipboard

Flag this post as spam?

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


  • Jimmy 19 posts 118 karma points
    Jun 19, 2024 @ 12:50
    Jimmy
    0

    Get the home node in current site

    Hi

    I'm using Umbraco 13. My structure is

    Site1
    --pages
    ---more pages 
    Site2
    --pages
    ---more pages
    Site3
    --pages
    ---more pages
    

    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?

  • Simon Napper 113 posts 347 karma points
    Jun 19, 2024 @ 13:26
    Simon Napper
    1

    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.

  • Jimmy 19 posts 118 karma points
    Jun 19, 2024 @ 13:29
    Jimmy
    0

    Hi

    I don't have currentPage. Which interface could I use to inject this?

  • Simon Napper 113 posts 347 karma points
    Jun 19, 2024 @ 13:32
    Simon Napper
    0

    Hi Jimmy,

    Are you trying to do this in Razor, a RenderController, SurfaceController, custom api, etc?

    Will affect how you can access it.

  • Jimmy 19 posts 118 karma points
    Jun 19, 2024 @ 13:34
    Jimmy
    0

    It's a custom class.

    Thanks

  • Simon Napper 113 posts 347 karma points
    Jun 19, 2024 @ 13:43
    Simon Napper
    1

    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>();
    
  • Jimmy 19 posts 118 karma points
    Jun 19, 2024 @ 14:01
    Jimmy
    0

    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.

  • Simon Napper 113 posts 347 karma points
    Jun 19, 2024 @ 14:33
    Simon Napper
    0

    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.?

  • Jimmy 19 posts 118 karma points
    Jun 19, 2024 @ 14:47
    Jimmy
    0

    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

  • Simon Napper 113 posts 347 karma points
    Jun 19, 2024 @ 15:07
    Simon Napper
    0

    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?

  • Jimmy 19 posts 118 karma points
    Jun 19, 2024 @ 16:28
    Jimmy
    0

    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.

  • Simon Napper 113 posts 347 karma points
    Jun 19, 2024 @ 17:30
    Simon Napper
    0

    Do you want to post some of your code up on here? Might help us understand a bit more what your aiming for.

Please Sign in or register to post replies

Write your reply to:

Draft