Copied to clipboard

Flag this post as spam?

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


  • Louis 4 posts 84 karma points
    Oct 29, 2021 @ 15:34
    Louis
    0

    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:

    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:

    ///<summary>
    /// Ticket Page Configuration Items
    ///</summary>
    
    
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Umbraco.ModelsBuilder.Embedded", "8.17.0")]
        [ImplementPropertyType("ticketPageConfigurationItems")]
        public virtual global::System.Collections.Generic.IEnumerable<global::Team.Meta.Core.Models.Content.TicketPageConfigurationItem> TicketPageConfigurationItems => this.Value<global::System.Collections.Generic.IEnumerable<global::Team.Meta.Core.Models.Content.TicketPageConfigurationItem>>("ticketPageConfigurationItems");
    

    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

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Oct 31, 2021 @ 17:20
    Bjarne Fyrstenborg
    101

    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:

    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
         }
    }
    
  • Jun 12 posts 92 karma points
    Nov 04, 2021 @ 03:37
    Jun
    0

    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

    enter image description here

  • Bjarne Fyrstenborg 1284 posts 4038 karma points MVP 8x c-trib
    Nov 04, 2021 @ 10:49
    Bjarne Fyrstenborg
    1

    Hi Jun

    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.

  • Jun 12 posts 92 karma points
    Nov 07, 2021 @ 23:36
    Jun
    0

    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.

    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.

Please Sign in or register to post replies

Write your reply to:

Draft