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 590 posts 2366 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 108 posts 287 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 590 posts 2366 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 590 posts 2366 karma points
    Jun 26, 2019 @ 14:16
    Bo Jacobsen
    103

    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 108 posts 287 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 590 posts 2366 karma points
    Jun 26, 2019 @ 14:39
    Bo Jacobsen
    1

    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.

  • David Armitage 503 posts 2071 karma points
    Jun 14, 2020 @ 04:01
    David Armitage
    0

    Hi Guys,

    Here is how I did it to get a photo from the !Member object.

    1. Here is how to get the media item UDI.

      var photoUdi = member.GetValue

    2. And here is how to get the media using the UDI. I saw examples converting the UDI to a guid then using the guid to get the media item. This is not nesessary. You can use the UDI itself in Umbraco to get the media. Eg.

      public IPublishedContent GetMediaByUdi(Udi udi)
      {
          IPublishedContent media = null;
      
      
      
      if(udi != null)
      {
          using (UmbracoContextReference umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext())
          {
              IPublishedMediaCache mediaHelper = umbracoContextReference.UmbracoContext.Media;
              media = mediaHelper.GetById(udi);
          }
      }
      
      
      return media;
      
      }

    Hope this helps someone.

    Regards

    David

  • Bo Jacobsen 590 posts 2366 karma points
    Jun 15, 2020 @ 14:41
    Bo Jacobsen
    1

    Hi David.

    Seems like they extended the GetById method to also accept a udi. So the right way nowadays is proberly this.

    public IPublishedContent Content(Udi udi)
    {
        using (var cref = Context.EnsureUmbracoContext())
        {
            return cref.UmbracoContext.Content.GetById(udi);
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft