Copied to clipboard

Flag this post as spam?

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


  • Bo Jacobsen 293 posts 1281 karma points
    Jun 26, 2019 @ 10:46
    Bo Jacobsen
    0

    How to get content or media by UDI from IUmbracoContextFactory

    Hi all.

    Is there a way to get content and media by UDI in custom services?

    public class CustomService : ICustomService
    {
        private readonly IUmbracoContextFactory Context;
    
        public CustomService(IUmbracoContextFactory context)
        {
            Context = context;
        }
    
        public IPublishedContent Content(Udi udi)
        {
            using (var cref = Context.EnsureUmbracoContext())
            {
                var cache = cref.UmbracoContext.ContentCache;
                var node = cache.GetById(udi); // This do not work!
                return node;
            }
        }
    }
    
  • George Phillipson 61 posts 176 karma points
    Jun 26, 2019 @ 11:54
    George Phillipson
    0

    Hi Bo Jacobsen

    public IPublishedContent ImageUDi(Guid udi)
            {
    
                using (var cf = _contextFactory.EnsureUmbracoContext())
                {
                    var cache = cf.UmbracoContext.ContentCache;
                   IPublishedContent node = cache.GetById(udi);
    
                   return node;
                }
            }
    
    var displayUrl = _helperService.ImageUDi(new Guid("ea162e11-3a74-45b6-abc8-0451d4bc2b41")).Value<IPublishedContent>("image").Url;
    

    Returns image for me

    enter image description here

    Regards George

  • Bo Jacobsen 293 posts 1281 karma points
    Jun 26, 2019 @ 14:04
    Bo Jacobsen
    0

    Hi George.

    Thanks for answering.

    How do you auto convert your Udi to a Guid?

    Udi - umb://document/4fed18d8c5e34d5e88cfff3a5b457bf2

    Guid - 4fed18d8-c5e3-4d5e-88cf-ff3a5b457bf2

  • Bo Jacobsen 293 posts 1281 karma points
    Jun 26, 2019 @ 14:16
    Bo Jacobsen
    100

    I found a way

    public class CustomService : ICustomService
    {
        private readonly IUmbracoContextFactory Context;
    
        public CustomService(IUmbracoContextFactory context)
        {
            Context = context;
        }
    
        public IPublishedContent Content(Udi udi)
        {
            using (var cref = Context.EnsureUmbracoContext())
            {
                var udiAsString = udi.ToString();
                var guid = Guid.ParseExact(udiAsString.Substring(udiAsString.Length - 32), "N");
                var cache = cref.UmbracoContext.ContentCache;
                var node = cache.GetById(guid);
                return node;
            }
        }
    }
    
  • George Phillipson 61 posts 176 karma points
    Jun 26, 2019 @ 14:29
    George Phillipson
    0

    Hi Bo Jacobsen

    What I do is get the key, which is the Guid.

     var homeNode = Umbraco.AssignedContentItem;
                Guid udi = new Guid(homeNode.Key.ToString());
                var displayUrl = _helperService.ImageUDi(udi).Value<IPublishedContent>("image").Url;
    

    One strange thing I noticed was all properties have the same UDI as the key, see image attached, hopefully, someone with a better understanding of how the internal work can explain why.enter image description here

    Hope it helps

    Regards George

  • Bo Jacobsen 293 posts 1281 karma points
    Jun 26, 2019 @ 14:39
    Bo Jacobsen
    0

    Hi George.

    I found a way, but i just wanted to respond to your reply.

    An Umbraco UDI consists of three parts: the scheme, the type and a GUID Identifier. For example: umb://document/4fed18d8c5e34d5e88cfff3a5b457bf2.

    And since i only have access to the Udi, not the id or the key, then i needed a way to get it.

Please Sign in or register to post replies

Write your reply to:

Draft