Copied to clipboard

Flag this post as spam?

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


  • MB 68 posts 264 karma points
    Feb 07, 2019 @ 05:08
    MB
    0

    Find RootNode in ApplicationStarting

    I need to make a configuration decision in ApplicationStarting() based on a property-value of a node.

    To get that node/value I would normally find the rootnode(s) using UmbracoHelper(UmbracoContext.Current) and work from there.

    However, I find that UmbracoContext.Current is null at that point, and so that approach won't work.

    Is there a way around this, or is it game-over?

  • Alex Skrypnyk 5273 posts 19288 karma points MVP 3x admin c-trib
    Feb 07, 2019 @ 09:22
    Alex Skrypnyk
    100

    Hi MB

    Try to use ContentService:

    var firstRootNode = ApplicationContext.Services.ContentService.GetByLevel(1).FirstOrDefault();
    

    Thanks,

    Alex

  • Alex Skrypnyk 5273 posts 19288 karma points MVP 3x admin c-trib
    Feb 07, 2019 @ 09:23
    Alex Skrypnyk
    0

    no game overs with Umbraco !!!

  • Dirk De Grave 4532 posts 5977 karma points MVP 3x admin
    Feb 07, 2019 @ 09:41
    Dirk De Grave
    0

    have you tried moving your code to the ApplicationStarted event, the umbraco context might not be ready on ApplicationStarting, hence the naming of the events...

    using ContentService is not considered best practice imho

  • Alex Skrypnyk 5273 posts 19288 karma points MVP 3x admin c-trib
    Feb 07, 2019 @ 09:44
    Alex Skrypnyk
    0

    I don't see a big problem to use a content service on application starting event, it's not a page loading event

    Dirk, do you think that only IPublishedContent is the best way?

  • Dirk De Grave 4532 posts 5977 karma points MVP 3x admin
    Feb 07, 2019 @ 09:49
    Dirk De Grave
    1

    i didn't say it's a problem (1sql call (probably) won't make a difference), but avoid sql round trips whenever you can, which is what content service will do whereas the umbraco helper won't... Consider implementing this code in 1, 2, 50, 1000 event handlers on startup (yes, i'm playing devil's advocate). I just wanted to hightlight the difference

  • MB 68 posts 264 karma points
    Feb 07, 2019 @ 10:33
    MB
    1

    Cheers guys.

    The action I need to take needs to be done during the ApplicationStarting event, so I pretty much assumed that UmbracoContext was off the table at that point - or I should say I did after the usual 'Doh!'

    I'll try using ContentService, as I don't think that any performance hit is likely to be an issue for me during the ApplicationStart phase.

  • MB 68 posts 264 karma points
    Feb 08, 2019 @ 00:51
    MB
    0
    var firstRootNode = ApplicationContext.Services.ContentService.GetByLevel(1).FirstOrDefault();
    

    Actually... you can't do that either.

    "Resolution is not frozen, it is not yet possible to get values from it."

    On checking the documentation (when all else fails, RTFM)

    "Never execute any logic or access any Umbraco services in this method"

    That's fairly explicit, so it appears that maybe it is actually game-over for accessing that content in the ApplicationStarting event-handler.

    However, although I still don't actually understand the relationship of the Umbraco ApplicationStart* events and the global Application_Start event, on doing a bit of testing, it appears that I can actually achieve what I needed (add to the global filters) in the Umbraco ApplicationStarted event-handler, where I can use UmbracoContext and hence UmbracoHelper.

    So... problem side-stepped.

Please Sign in or register to post replies

Write your reply to:

Draft