I am trying to get a list of values back from IContent using the following but every time it is null:
var config = _contentService.GetContentOfContentType(TicketPageConfiguration.ModelTypeAlias)
.FirstOrDefault()
var Items = config.GetValue<IEnumerable<TicketPageConfigurationItem>>("ticketPageConfigurationItems");
As you can see I am using the correct names but every time it comes back with null
My generated model with the variable I trying to access:
I can easily use GetValue for int or bool but not an object. Does anyone have any idea where I am going wrong? If it helps this is a nested content type
You don't need to using ContentService unless you are doing CRUD operations.
General speaking you would just request to data from the content cache using the following:
var items = Model.Value<IEnumerable<TicketPageConfigurationItem>>("ticketPageConfigurationItems");
or using ModelsBuilder:
var items = Model.TicketPageConfigurationItems;
In case you need to fetch these data from another node, like Home Page or root node, you can access this like the following.
var homePage = Model.Root() as HomePage;
or
var homePage = Model.Root<HomePage>();
var items = homePage.TicketPageConfigurationItems;
If you are working with this in a controller you could inject UmbracoHelper, IUmbracoContextAccessor or IUmbracoContextFactory or IPublishedContentQuery.
public class ProductSurfaceController : SurfaceController
{
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
public ProductSurfaceController(IUmbracoContextAccessor umbracoContextAccessor)
{
_umbracoContextAccessor = umbracoContextAccessor;
}
public ActionResult Index()
{
var cache = _umbracoContextAccessor.UmbracoContext.Content;
// Do something
}
}
I would like to thank you for your comprehensive guide to getting content.
However, the question still exists. I have a similar situation, I would be much appreciated if you can help wit the following:
As the screenshot below, I am trying to get the content in a NotificationHandle. There is IContent object returned - called node, and I want to access its content. The normal GetValue method returns a Udi, however, the generic GetValue method returns null.
Note sure which datatype / property editor the activeAppNotification property is using. Is it a custom property editor? In that case cast to AppNotification won't work unless you have a PVC (property value converter to resolve the datatype, eg. fetching IPublishedContent from cache using the udi.
https://our.umbraco.com/documentation/extending/property-editors/value-converters
However since you are dealing with IContent here, you probably need to either lookup the IContent using ContentService or from content cache returning IPublishedContent.
You could try injecting IContentService, IUmbracoContextAccessor or IUmbracoContextFactory in the contructor of AppPushNotification and then get the IContent or IPublishedContent from the udi.
Thank you very much for the quick useful response. It clearly points me to the right way!
As a result, I can do what I want to do now. Thanks again for your detailed explanation!! It is much appreciated.
Below is what I did to get the content using ContentService.
var activeAppNotification1 = node.GetValue("activeAppNotification");
if (UdiParser.TryParse<GuidUdi>(activeAppNotification1.ToString(), out GuidUdi newGuidUdi1))
{
var getContent = _contentService.GetById(newGuidUdi1.Guid);
}
Besides, I am curious about the difference for IContentService, IUmbracoContextAccessor and IUmbracoContextFactory, and wondering when to use what, and their performance.
Do you know any articles that explain them all? I will try to find out as well, but thanks in advance if you have anything in place that can be shared esily.
IContent GetValue is always null
I am trying to get a list of values back from IContent using the following but every time it is null:
As you can see I am using the correct names but every time it comes back with null
My generated model with the variable I trying to access:
I can easily use GetValue for int or bool but not an object. Does anyone have any idea where I am going wrong? If it helps this is a nested content type
Thanks
Hi Louis
You don't need to using
ContentService
unless you are doing CRUD operations.General speaking you would just request to data from the content cache using the following:
or using ModelsBuilder:
In case you need to fetch these data from another node, like Home Page or root node, you can access this like the following.
or
If you are working with this in a controller you could inject
UmbracoHelper
,IUmbracoContextAccessor
orIUmbracoContextFactory
orIPublishedContentQuery
.Hi Bjarne,
I would like to thank you for your comprehensive guide to getting content.
However, the question still exists. I have a similar situation, I would be much appreciated if you can help wit the following:
As the screenshot below, I am trying to get the content in a NotificationHandle. There is IContent object returned - called node, and I want to access its content. The normal GetValue method returns a Udi, however, the generic GetValue method returns null.
Can you please point me to the right direction?
Thanks in advance, June
Hi Jun
Note sure which datatype / property editor the
activeAppNotification
property is using. Is it a custom property editor? In that case cast toAppNotification
won't work unless you have a PVC (property value converter to resolve the datatype, eg. fetchingIPublishedContent
from cache using the udi. https://our.umbraco.com/documentation/extending/property-editors/value-convertersHowever since you are dealing with
IContent
here, you probably need to either lookup theIContent
usingContentService
or from content cache returningIPublishedContent
.You could try injecting
IContentService
,IUmbracoContextAccessor
orIUmbracoContextFactory
in the contructor ofAppPushNotification
and then get theIContent
orIPublishedContent
from the udi.Hi Bjarne,
Thank you very much for the quick useful response. It clearly points me to the right way!
As a result, I can do what I want to do now. Thanks again for your detailed explanation!! It is much appreciated.
Below is what I did to get the content using ContentService.
Besides, I am curious about the difference for
IContentService
,IUmbracoContextAccessor
andIUmbracoContextFactory
, and wondering when to use what, and their performance.Do you know any articles that explain them all? I will try to find out as well, but thanks in advance if you have anything in place that can be shared esily.
is working on a reply...