Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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.
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.
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.
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?
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
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!
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 working on a reply...
Write your reply to:
Image will be uploaded when post is submitted