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
Was wondering if there's a way you can map IContent to the generated Models Builder model?
Anyone did anything similar before?
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.
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;
This sadly doesn't work in Umbraco 8 anymore :( Do you have an idea how you can do it there?
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:
public class PreviewApiController : UmbracoApiController
private readonly Lazy<IContentResolver> _contentResolver;
private readonly IPublishedSnapshotService _publishedSnapshotService;
private readonly IUserService _userService;
private readonly IContentService _contentService;
public PreviewApiController(Lazy<IContentResolver> contentResolver, IPublishedSnapshotService publishedSnapshotService, IUserService userService, IContentService contentService)
_contentResolver = contentResolver;
_publishedSnapshotService = publishedSnapshotService;
_userService = userService;
_contentService = contentService;
public IHttpActionResult Get(Guid id)
IPublishedContent content = Umbraco.Content(id);
ContentModel contentModel = _contentResolver.Value.ResolveContent(content);
First of all thanks for the fast response!
This is not working for me
var test = _umbracoHelper.Content(productId);
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 :(
I have also asked question in another thread.
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...
Write your reply to:
Image will be uploaded when post is submitted