Copied to clipboard

Flag this post as spam?

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


  • Paul Sterling 712 posts 1446 karma points MVP 4x admin c-trib
    May 04, 2018 @ 02:36
    Paul Sterling
    0

    Get Site Root in ApiController with Multi-Root Site FIltering by Doc Type

    In all the time I've been using Umbraco I can't believe I've never come across this before. Of course I can get the current site root given only a specific node Id, but it's ugly and brute force. So I get to ask a forum question:

    In an ApiController (so I don't have UmbracoContext.Current.PublishedContentRequest, Model, etc...) what is the best (or a good) way to get the root node of a specific type for the current site given I have a multi domain instance?

    I am passing the current node Id to the controller, and that is all I know about which site in this instance the Api call is coming from.

    I'd love to have more insight...thank you in advance!

  • Dave Woestenborghs 3213 posts 10799 karma points MVP 4x admin c-trib
    May 04, 2018 @ 06:48
    Dave Woestenborghs
    100

    Hi Paul,

    Since you got the id of the node, I would say xpath query. Something like this :

    var node = UmbracoContext.Current.ContentCache.GetSingleByXPath($"id({id})[@isDoc]/ancestor-or-self::{ancestorDoctypeAlias}");
    

    Dave

  • Paul Sterling 712 posts 1446 karma points MVP 4x admin c-trib
    May 04, 2018 @ 14:42
    Paul Sterling
    0

    That's exactly it, thank you. That is much more elegant than what I had mangled without the XPath. For posterity, here's what I have:

            homepage homeNode = UmbracoContext.Current.ContentCache
                .GetSingleByXPath($"id({id})[@isDoc]/ancestor-or-self::{homepage.ModelTypeAlias}") 
                as homepage;
    
            globalSettings settings = node.WebsiteConfigurationNode.Children
                .Where(x => x.ContentType.Alias == globalSettings.ModelTypeAlias).First() 
                as globalSettings;
    
            var testimonialHomeId = settings.TestimonialRepository.Id;
    
Please Sign in or register to post replies

Write your reply to:

Draft