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 116 posts 370 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 116 posts 370 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 116 posts 370 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 116 posts 370 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 116 posts 370 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 116 posts 370 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.

  • Richard Hamilton 12 posts 83 karma points
    Oct 25, 2024 @ 16:08
    Richard Hamilton
    0

    Simon

    I have a similar issue. And the issue is that the current page can be a shared / reusuable content page like a product that is displayed on various sites.

    So each site has it's own homenode but if you are looking up by the current page (product node ID), it wouldn't know which one to lookup.

  • Farshad 8 posts 78 karma points
    Nov 05, 2024 @ 09:53
    Farshad
    0

    if your code is running out side the current HTTP request pipeline (like in a background task or scheduled job) the context will be null . please check if that is the case or it may have something to do with the proper DI.

  • Simon Napper 116 posts 370 karma points
    Nov 05, 2024 @ 10:08
    Simon Napper
    0

    So this might help with your problem Richard and perhaps your Jimmy, but you might want to consider using a ContentFinder to help you find your home page.

    If you use IContentFinder and use the TryFindContent method, you can start to use things like finding by URL, etc, which might help with your routing?

    https://docs.umbraco.com/umbraco-cms/reference/routing/request-pipeline/icontentfinder

    Difficult to know if this will help out, but worth knowing about anyway!

Please Sign in or register to post replies

Write your reply to:

Draft