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 292 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 4026 posts 15836 karma points MVP 13x 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 4908 posts 12265 karma points MVP 5x 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 6x 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 4908 posts 12265 karma points MVP 5x 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 6x 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 403 posts 907 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 6x 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!

  • Tito 314 posts 623 karma points
    Nov 04, 2022 @ 12:00
    Tito
    0

    Is there a way to convert IContent to IPublishedContent for Umbraco 10? I have tried to update the Jeroen code with no luck :(

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Nov 04, 2022 @ 12:16
    Lee Kelleher
    1

    Hi Tito,

    Do you need to convert an IContent to IPublishedContent? As the content-cache (since v8) stores both the Saved and Published state of the content. You can access the saved (but published) state by using the preview flag.

    var content = UmbracoContext.Content.GetById(true, 1234);
    

    Otherwise, as Jeroen links to Matt's code snippets. There's also a blog post to go along with it: https://dev.to/mattbrailsford/converting-icontent-to-ipublishedcontent-in-umbraco-v8-3m7i

  • Tito 314 posts 623 karma points
    Nov 07, 2022 @ 09:26
    Tito
    0

    Thanks! thats exactly what i was looking for!

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Nov 04, 2022 @ 12:14
    Jeroen Breuer
    0

    Hi,

    There are some gists available for v8 and v9:

    https://gist.github.com/mattbrailsford/5f9638d357df59aeac1be8588a06c31e

    https://gist.github.com/mattbrailsford/cfce147963103dc47247454796840dc6

    The v9 version probably also works for v10.

    Jeroen

Please Sign in or register to post replies

Write your reply to:

Draft