Copied to clipboard

Flag this post as spam?

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


  • Mikkel Johansen 116 posts 291 karma points
    Dec 04, 2015 @ 10:43
    Mikkel Johansen
    0

    IContent property value

    Is it possible to typecast a NestedContent property when getting the value through a IContent property

    Example (that does not work) "items" is null

    var service = ApplicationContext.Current.Services.ContentService;
    var node =  service.GetById(nodeId);
    
    var items = node.GetValue<IEnumerable<IPublishedContent>>("products");
    

    If I try with node.GetValue("products") a fine JSON is returned.

    I have tried using the UmbracoHelper and TypedContent to get a IPublishedContent. But at that moment in code there is no "UmbracoContext.Current".

    -Mikkel

  • Lee Kelleher 3960 posts 15294 karma points MVP 11x admin c-trib
    Dec 05, 2015 @ 23:55
    Lee Kelleher
    0

    Hi Mikkel,

    The raw JSON data is converted to an IPublishedContent list via NC's ProperyValueConverter. But that's only with published content, not from IContent properties.

    See the call to ConvertDataToSource in NC code.

    In order to prepare the raw JSON, you'd need to run it through NC's PropertyEditor call to ConvertDbToString method... see here.


    I'm curious what you're planning to do once you get the IEnumerable<IPublishedContent> items? No offence, it just seems interesting doing this from the IContent object.

    Cheers,
    - Lee

  • Jeroen Breuer 4861 posts 12139 karma points MVP 3x admin c-trib
    Aug 16, 2016 @ 14:11
    Jeroen Breuer
    5

    I needed to do the same thing. I have an unpublished item (so it's only available as IContent), but I want to do things like .GetPropertyValue<IEnumerable<IPublishedContent>>("courses") because it's a much easier API to use.

    I've wrote an extension method which can convert an IContent to an IPublishedContent: https://gist.github.com/jbreuer/dde3605035179c34b7287850c45cb8c9

    More info in this topic: https://our.umbraco.org/forum/extending-umbraco-and-using-the-api/77358-convert-icontent-to-ipublishedcontent

    So now I can do things like this:

    // Convert it to an IPublishedContent. So this IPublishedContent has the unpublished version.
    var publishedContent = content.ToPublishedContent();
    
    // Get the items which are selected on the node.
    var items = publishedContent.GetPropertyValue<IEnumerable<IPublishedContent>>("courses");
    

    I hope this will be useful for someone.

    Jeroen

  • Poornima Nayar 106 posts 276 karma points MVP 3x c-trib
    Jul 25, 2017 @ 00:29
    Poornima Nayar
    0

    Hi Jeroen,

    The content helper came across as very helpful. I am trying to convert some html to pdf upon the published event and my doc types heavily rely upon NC. Just was wondering whether you hit any issues with the helper at all?

    Poornima

  • Jeroen Breuer 4861 posts 12139 karma points MVP 3x admin c-trib
    Jul 25, 2017 @ 20:01
    Jeroen Breuer
    0

    Hello,

    The code has been in production for over a year and I've never had any issues. Also blogged about it here: https://24days.in/umbraco-cms/2016/umbraco-edge-case-stories/#convert

    Jeroen

  • Poornima Nayar 106 posts 276 karma points MVP 3x c-trib
    Jul 25, 2017 @ 20:08
    Poornima Nayar
    0

    The helper class is very useful. I am going to use that as well.

  • nickornotto 322 posts 694 karma points
    Apr 27, 2018 @ 01:00
    nickornotto
    0

    Brilliant Jeroen, your extension helped me to resolve the same problem. Thank you!

  • Saba Tandashvili 29 posts 181 karma points
    Feb 27, 2019 @ 14:33
    Saba Tandashvili
    0

    You rock, this class helped me so much, i was using JavaScriptSerializer.Deserialize for Icontent.Getvalue parse. Was a bit stupid way but working perfectly. Moved on your code since today, thanks for sharing.

  • Andrew Wilson 9 posts 79 karma points
    Mar 20, 2019 @ 11:35
    Andrew Wilson
    0

    Your extension just helped me solve a similar problem! Thank you Jeroen!

  • Poornima Nayar 106 posts 276 karma points MVP 3x c-trib
    Jul 25, 2017 @ 20:09
    Poornima Nayar
    0

    I think the core needs something of this kind. I have a quirky content structure due to the way the application needs to work and i am trying to do some processing (html->pdf) on save and publish. And this class has been helping me well so far!

Please Sign in or register to post replies

Write your reply to:

Draft