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".
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.
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.
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.
// 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");
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?
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.
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!
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);
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
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
Hi Mikkel,
The raw JSON data is converted to an
IPublishedContent
list via NC's ProperyValueConverter. But that's only with published content, not fromIContent
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 theIContent
object.Cheers,
- Lee
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:
I hope this will be useful for someone.
Jeroen
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
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
The helper class is very useful. I am going to use that as well.
Brilliant Jeroen, your extension helped me to resolve the same problem. Thank you!
You rock, this class helped me so much, i was using
JavaScriptSerializer.Deserialize
forIcontent.Getvalue
parse. Was a bit stupid way but working perfectly. Moved on your code since today, thanks for sharing.Your extension just helped me solve a similar problem! Thank you Jeroen!
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!
Is there a way to convert IContent to IPublishedContent for Umbraco 10? I have tried to update the Jeroen code with no luck :(
Hi Tito,
Do you need to convert an
IContent
toIPublishedContent
? 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 thepreview
flag.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
Thanks! thats exactly what i was looking for!
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
is working on a reply...