Copied to clipboard

Flag this post as spam?

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


  • Steve 140 posts 321 karma points
    Nov 26, 2019 @ 20:21
    Steve
    0

    Query content nodes in Umbraco 8

    Hi,
    I'm working on updating a v7 site to v8 and having trouble re-using some code that locates the root node and then queries descendants based on various characteristics. This is in a C# class file so had been using the UmbracoHelper - such as helper.ContentAtRoot().First() - which seems like it doesn't work in Umbraco 8.
    Does anyone have a suggestion?
    Thanks, Steve

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Nov 26, 2019 @ 23:41
    Alex Skrypnyk
    0

    Hi Steve

    I think this code should work:

    var rootNode = Model.Root().Siblings().First();
    

    Thanks,

    Alex

  • Steve 140 posts 321 karma points
    Nov 27, 2019 @ 00:04
    Steve
    0

    Hi Alex,
    Thanks for the reply.
    In this case when I use 'Model' I get an error 'The name Model does not exist in the current context'.
    I'm not in a view but instead writing some utility/task code to run on a schedule using something like Hangfire.
    I can't seem to figure out how to get the current context in Umbraco 8 or how to adapt other suggestions that I've come across such as this post.
    At this point I don't know if I'm simply missing a reference or just not going about acquiring the current context correctly.

    private readonly IUmbracoContextFactory _umbracoContextFactory;
    
            public void DoIt(PerformContext context)
            {
    
                UmbracoContextReference contextReference = _umbracoContextFactory.EnsureUmbracoContext();
    
                var myContent = contextReference.UmbracoContext.Content.GetAtRoot().FirstOrDefault();
    
             }
    


    Returns an 'Object Reference not set to instance of an object' error

    Steve

  • Steve 140 posts 321 karma points
    Nov 27, 2019 @ 03:13
    Steve
    0

    Hi everyone,
    Still working out how Umbraco 8 and Hangfire work together but this Our Forum post proved to be very helpful.
    Does anybody know where can we (who don't already know) can learn how to do this with Dependency Injection?
    Thanks everyone!
    Steve

Please Sign in or register to post replies

Write your reply to:

Draft