Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jay 413 posts 639 karma points
    Nov 21, 2018 @ 17:12
    Jay
    0

    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

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Nov 21, 2018 @ 17:26
    Nicholas Westby
    0

    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.

  • Søren Kottal 702 posts 4497 karma points MVP 5x c-trib
    Nov 22, 2018 @ 09:08
    Søren Kottal
    0

    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

    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;
    
  • Dave de Moel 122 posts 574 karma points c-trib
    May 03, 2019 @ 12:56
    Dave de Moel
    0

    This sadly doesn't work in Umbraco 8 anymore :( Do you have an idea how you can do it there?

  • Jarno 21 posts 152 karma points
    Dec 03, 2019 @ 09:57
    Jarno
    0

    Hi Dave,

    Do you have already found a solution for this in v8?

  • Dave de Moel 122 posts 574 karma points c-trib
    Dec 03, 2019 @ 10:02
    Dave de Moel
    0

    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;
        }
    
        [Route("{id:guid}")]
        [ResponseType(typeof(ContentModel))]
        public IHttpActionResult Get(Guid id)
        {
            _publishedSnapshotService.EnterPreview(_userService.GetUserById(-1), _contentService.GetById(id).Id);
    
            IPublishedContent content = Umbraco.Content(id);
    
            ContentModel contentModel = _contentResolver.Value.ResolveContent(content);
    
            return Ok(contentModel);
        }
    }
    
  • Jarno 21 posts 152 karma points
    Dec 03, 2019 @ 10:12
    Jarno
    0

    Hi Dave,

    First of all thanks for the fast response!

    This is not working for me

    _publishedSnapshotService.EnterPreview(_userService.GetUserById(-1), _contentService.GetById(productId).Id);
    var test = _umbracoHelper.Content(productId);
    

    the variable test is still null :(

  • Dave de Moel 122 posts 574 karma points c-trib
    Dec 03, 2019 @ 10:25
    Dave de Moel
    0

    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 :(

  • Jarno 21 posts 152 karma points
    Dec 03, 2019 @ 10:30
    Jarno
    0

    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 :)

  • Jay 413 posts 639 karma points
    Nov 22, 2018 @ 12:16
    Jay
    0

    thanks Guys will try it out ;)

Please Sign in or register to post replies

Write your reply to:

Draft