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 55 posts 260 karma points
    Nov 29, 2019 @ 18:45
    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 1451 posts 9716 karma points MVP 5x c-trib
    Nov 30, 2019 @ 09:47
    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 55 posts 260 karma points
    Nov 30, 2019 @ 11:24
    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 1451 posts 9716 karma points MVP 5x c-trib
    Nov 30, 2019 @ 15:32
    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 55 posts 260 karma points
    Nov 30, 2019 @ 19:10
    Johannes Lantz
    0

    Sorry about that!

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

    //Johannes

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies