Sort of. I've managed to map IContent (i.e., unpublished content nodes) to C# classes (similar to those generated by Models Builder) using Ditto. I essentially implemented the IPublishedContent interface in my own custom class. In this class, I had a constructor that accepts an IContent node, then it essentially simulates what an IPublishedContent node does.
You can convert from IContent to IPublishedContent using this gist (https://gist.github.com/jbreuer/dde3605035179c34b7287850c45cb8c9)
Since ModelsBuilder models are based on IPublishedContent you can do like this
using Project.Web.Core.Extensions; // the namespace of the extension method
// your IContent here
var content = new IContent();
// convert to IPublishedContent and cast as "YourModelsBuilderModel"
var modelledContent = content.ToIPublishedContent() as YourModelsBuilderModel;
Kind of. In Umbraco 8 you can use the SnapshotService to enter preview mode. If you then use the normal UmbracoHelper to get the content you get the saved, unpublished, content instead of the published content.
_publishedSnapshotService.EnterPreview(_userService.GetUserById(-1), _contentService.GetById(productId).Id);
var test = _umbracoHelper.Content(productId);
Hmm seems like in Umbraco 8.3 something changed.... Will have to dive into it to see if it is still possible but indeed the above doesn't work anymore :(
IContent to Models Builder
Was wondering if there's a way you can map IContent to the generated Models Builder model?
Anyone did anything similar before?
Thanks
Sort of. I've managed to map IContent (i.e., unpublished content nodes) to C# classes (similar to those generated by Models Builder) using Ditto. I essentially implemented the IPublishedContent interface in my own custom class. In this class, I had a constructor that accepts an IContent node, then it essentially simulates what an IPublishedContent node does.
I based it largely off of what Archetype does to fake an instance of IPublishedContent: https://github.com/kgiszewski/Archetype/blob/e5f09f8f1b822e28ea91b85ac1e2a4f321e40040/app/Umbraco/Umbraco.Archetype/Models/ArchetypePublishedContent.cs
My use case was very specific to Archetype / Ditto, so I won't post any code here, but hopefully that gives you some ideas to get started.
Hi
You can convert from IContent to IPublishedContent using this gist (https://gist.github.com/jbreuer/dde3605035179c34b7287850c45cb8c9)
Since ModelsBuilder models are based on IPublishedContent you can do like this
This sadly doesn't work in Umbraco 8 anymore :( Do you have an idea how you can do it there?
Hi Dave,
Do you have already found a solution for this in v8?
Kind of. In Umbraco 8 you can use the SnapshotService to enter preview mode. If you then use the normal UmbracoHelper to get the content you get the saved, unpublished, content instead of the published content.
I use that now for my own headless api package:
Hi Dave,
First of all thanks for the fast response!
This is not working for me
the variable test is still null :(
Hmm seems like in Umbraco 8.3 something changed.... Will have to dive into it to see if it is still possible but indeed the above doesn't work anymore :(
Hi Dave,
I have also asked question in another thread. https://our.umbraco.com/forum/using-umbraco-and-getting-started/100238-casting-icontent-to-ipublishedcontent-in-v8
If I found a solution I let you know. Anyway thanks for you help :)
thanks Guys will try it out ;)
is working on a reply...