Copied to clipboard

Flag this post as spam?

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


  • Johannes Lantz 34 posts 208 karma points
    1 week ago
    Johannes Lantz
    0

    Neasted content value in controller

    Hello Evryone!

    I have gotten a bit stuck here and looking for som guideance.

    So I have an Neasted content data type. And I want to fetch the values in a controller.

    I can fetch the first Neasted content byt doing something like this var foo = settingsNode.Properties.Where(x => x.PropertyTypeAlias == backgroundPickerAlias); and that becomes an Umbraco.Web.PublishedCache.XmlPublishedCache.XmlPublishedProperty. But I am having touble fetching the values. By doing foo .Value I get object <string>, but I can't figure out how to get the value of the object. I have been trying to do some LINQ statement, like "Select" and "Where" but it says 'object' does not contain a definition for 'Where' and the best extension method overload PublishedContentExtensions.Where(IEnumerable<IPublishedContent>, string)' requires a receiver of type 'IEnumerable<IPublishedContent>'

    I can't figure this out to save my life..

    I am open to everything!

    //Johannes

  • Marc Goodson 1024 posts 6755 karma points MVP 4x c-trib
    1 week ago
    Marc Goodson
    0

    Hi Johannes

    You can retrieve a Nested Content's property values in the format of an IEnumerable of IPublishedContent - there is an individual IPublishedContent item representing each repeating Nested Content Item.

    Have a look at the reference page in the docs for some examples and explanation:

    https://our.umbraco.com/Documentation/Getting-Started/Backoffice/Property-Editors/Built-in-Property-Editors/Nested-Content/index-v7

    But essentially if your NestedContent Property type has alias "backgroundItems"

    you would be able to get the list of repeating items like so:

    var allBackgroundItems = settingsNode.GetPropertyValue<IEnumerable<IPublishedContent>>("backgroundItems");
    

    then you could loop through each of these items and read the individual properties for each repeated element eg

    @foreach (var backgroundItem in allBackgroundItems){
           //use the alias of the properties of the repeating document type to read the values eg: if there were a 'title' property
    
                   var backgroundItemTitle = 
                   backgroundItem.GetPropertyValue<string>("title");
    
                   var backgroundImage = 
                   backgroundItem.GetPropertyValue<IPublishedContent>("backgroundImagePicker");
    
       }
    

    unless I've misunderstood what you are trying to do!

    regards

    marc

  • Johannes Lantz 34 posts 208 karma points
    1 week ago
    Johannes Lantz
    0

    Hi Marc!

    Thank you for your reply!

    What I am trying to achive is to get the background Image in a Neasted Content, on the newly saved node and save that url somwhere.

    So I am using theCustomApplicationEventHandler for the "on save event".

    I have tried your suggested method, but I would only recive the Published content, but it's not published yet, So this would only work if I where to save and publish 2 times in a row. That's why I trying to get the value via the "Properties" value.

    //Johannes

  • Marc Goodson 1024 posts 6755 karma points MVP 4x c-trib
    1 week ago
    Marc Goodson
    1

    Hi Johannes

    ahh yes, sorry I misunderstood, context is everything!

    I think, if you are working with an IContent object in the Save event the value, will currently be a blob of JSON for the Nested Content property you are saving, and so you will need to deserialise it first to start reading the values:

    var nestedItems = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(settingsNode.GetValue<string>("nestedContentPropertyAlias"));
    

    then you can loop through the list of dictionary items and their values...

    hope that gets you further down the line!

    regards

    Marc

  • Johannes Lantz 34 posts 208 karma points
    1 week ago
    Johannes Lantz
    0

    Sorry about that!

    You are an absolute legend Marc! Thank you so much!

    //Johannes

Please Sign in or register to post replies

Write your reply to:

Draft