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.
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.
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?
Hi J
In Umbraco 8 / Umbraco 9 and Umbraco 10+ the method
TypedContent()
no longer exists. Instead it is justContent()
.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
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
In which case if i attempt to create a new instance i need to pass in
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
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 ofUmbracoHelper
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: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.is working on a reply...