Copied to clipboard

Flag this post as spam?

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

  • Remko 60 posts 158 karma points
    Mar 11, 2019 @ 11:16

    v8 - UmbracoHelper, easy way to create instance?

    In our code we were using this a lot in v7:

    UmbracoHelper umbraco = new UmbracoHelper(UmbracoContext.Current);
    IPublishedContent node = umbraco.Content(nodeId);

    But for v8 I cannot find any documentation on how to do this the new way. I just want to grab a node as IPublishedContent in back-end code...

    Can anyone help me with this?

  • Louis Jackson-Rees 94 posts 324 karma points c-trib
    Mar 11, 2019 @ 11:39
    Louis Jackson-Rees

    I think this should all be using Dependency Injection now, but there is a static method on the Current object (you may need to add a reference):

    using Umbraco.Web.Composing;    

    This may not be the 100% spot on syntax but there is definitely a way of accessing an umbraco helper on this object.

    the umbraco context can be called in a similar way.

    I will update this with the actual code when i am back at my PC

  • Sebastiaan Janssen 4815 posts 14087 karma points MVP admin hq
    Mar 11, 2019 @ 12:01
    Sebastiaan Janssen

    But please don't use Current - in most places you will use this kind of code (like an API controller, or a SurfaceController, etc). you can just access these things already:

    IPublishedContent node = this.UmbracoContext.ContentCache.GetById(1234);

    If that's not available you can almost always get the UmbracoContext through dependency injection, I wrote a bit more about that over here:

    Happy to look at some code if you can't figure it out!

  • Sebastiaan Janssen 4815 posts 14087 karma points MVP admin hq
    Mar 11, 2019 @ 13:12
    Sebastiaan Janssen

    Ah.. it's even easier, if you're using any of our base classes like SurfaceController etc. then you just do:

    IPublishedContent content = Umbraco.Content(1234);
  • Remko 60 posts 158 karma points
    Mar 13, 2019 @ 14:31

    @Sebastiaan, IContentService as parameter for constructor seems to work, but when adding UmbracoHelper to constructor the magical dependency injection doesn't seem to work ("Boot failed")

    Is there any way to get UmbracoHelper available in IComponent ?

    enter image description here

  • Sebastiaan Janssen 4815 posts 14087 karma points MVP admin hq
    Mar 13, 2019 @ 15:13
    Sebastiaan Janssen

    @Remko - I understand from Stephane that you're not actually meant to try to get an UmbracoHelper in an IComponent, so the question is: what do you need it for?

    I'm guessing the most common scenario would be to query some content, in which case you can inject IUmbracoContextFactory and get an umbracoContext from it, the safe way to do that (in case the context does not exist yet) is to Ensure it exists first:

        private readonly IContentService _contentService;
        private readonly IUmbracoContextFactory _context;
        public MyComponent(IContentService contentService, IUmbracoContextFactory context)
            _contentService = contentService;
            _context = context;
        public void Initialize()
            using (var cref = _context.EnsureUmbracoContext())
                var cache = cref.UmbracoContext.ContentCache;
                var node = cache.GetById(1234);
  • Ben Palmer 151 posts 732 karma points c-trib
    Apr 16, 2019 @ 21:03
    Ben Palmer

    I'm also looking to get an instance of UmbracoHelper, but in a custom service.

    The reason for me, which means I can't use the content cache, is that I need to get the current page. Usually, I would get AssignedContentItem from the Umbraco helper.

    Is there another way to get the current page?

Please Sign in or register to post replies

Write your reply to: