Copied to clipboard

Flag this post as spam?

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


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

    Casting iContent to iPublishedContent in v8

    Hi all,

    Is there anyone that has a solution for casting iContent to iPublishedContent in v8?

    In v7 is was using this extension method but it don't works in v8. https://gist.github.com/jbreuer/dde3605035179c34b7287850c45cb8c9

    Thanks!

  • Dave de Moel 122 posts 574 karma points c-trib
    Dec 04, 2019 @ 08:52
    Dave de Moel
    1

    Hey man,

    After the conversation yesterday I decided to check it out a bit with the snapshotservice and I figured out what changed thanks to Nathan Woulfe on the Slack community.

    Seems they slightly changed how to get preview IContent content. Here is an excerpt from my content api package:

    [RoutePrefix("api/preview")]
    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 = _publishedSnapshotService.CreatePublishedSnapshot(
                    _publishedSnapshotService.EnterPreview(
                        _userService.GetUserById(-1),
                        _contentService.GetById(id).Id))
                .Content.GetById(id);
    
            ContentModel contentModel = _contentResolver.Value.ResolveContent(content);
    
            return Ok(contentModel);
        }
    }
    

    I just verified this works in Umbraco 8.3

Please Sign in or register to post replies

Write your reply to:

Draft