Copied to clipboard

Flag this post as spam?

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


  • J 445 posts 862 karma points
    Oct 21, 2022 @ 12:31
    J
    0

    How to access Umbraco.TypedContent

    Im trying to access Umbraco.TypedContent in Umbraco 10 but cant find the same method when searching https://github.com/umbraco/Umbraco-CMS.

    Where can i find this now or what is the equivalent for this method?

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Oct 21, 2022 @ 13:28
    Nik
    2

    Hi J

    In Umbraco 8 / Umbraco 9 and Umbraco 10+ the method TypedContent() no longer exists. Instead it is just Content().

    You can see this here: https://github.com/umbraco/Umbraco-CMS/blob/b69afe81f3f6fcd37480b3b0295a62af44ede245/src/Umbraco.Web.Common/UmbracoHelper.cs#L173

    This was simplified as in v8 the support for Dynamics was removed removing the need for two different methods on the Umbraco Helper for getting content items.

    Hope that helps :-)

    Nik

  • J 445 posts 862 karma points
    Oct 21, 2022 @ 14:12
    J
    0

    Thanks Nik

    The way i see this is UmbracoHelper is a class which requires to be instantiated.

    Is the recommended approach to declare a new instance within the method/controller/class?

    Example

    public IActionResult Index(.....)
    {
    
      Umbraco.Cms.Web.Common.UmbracoHelper myHelper;
    
      myHelp.Content(....)
    }
    

    In which case if i attempt to create a new instance i need to pass in

    ICultureDictionaryFactory cultureDictionary,
            IUmbracoComponentRenderer componentRenderer,
            IPublishedContentQuery publishedContentQuery
    

    So i assume i would need to set up the above in my DI class? Just seems a very long way around to do this if thats correct?

    Thank you

  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Oct 22, 2022 @ 16:16
    Anders Bjerner
    100

    The UmbracoHelper class wraps a different resources, so if you don't already have an instance (eg. when in a Razor view), it might be better to access the needed resource directly instead, as creating new instances of UmbracoHelper might have an overhead.

    For instance, if you need to access published content, you can use the content cache directly instead. The content cache is available through the IUmbracoContext interface.

    Depending on where you code runs, an Umbraco context night not be available, but it should be when in a controller as in your example.

    In a controller you can inject IUmbracoContextAccessor, and then access the current Umbraco context as shown in the example below:

    using System;
    using Microsoft.AspNetCore.Mvc;
    using Umbraco.Cms.Core.Models.PublishedContent;
    using Umbraco.Cms.Core.Web;
    using Umbraco.Cms.Web.Common.Controllers;
    
    namespace Hello.Controllers {
    
        public class HelloController : UmbracoApiController {
    
            private readonly IUmbracoContextAccessor _umbracoContextAccessor;
    
            public HelloController(IUmbracoContextAccessor umbracoContextAccessor) {
                _umbracoContextAccessor = umbracoContextAccessor;
            }
    
            public IActionResult Index() {
    
                if (!_umbracoContextAccessor.TryGetUmbracoContext(out IUmbracoContext? umbraco))
                    throw new Exception("Umbraco context currently unavailable.");
    
                IPublishedContent? content = umbraco.Content?.GetById(1234);
    
            }
    
        }
    
    }
    

    If you're running your code in a background thread, an Umbraco context probably won't be available, so there is also an IUmbracoContextFactory you can inject instead.

Please Sign in or register to post replies

Write your reply to:

Draft