Copied to clipboard

Flag this post as spam?

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


  • Melvin Chew 10 posts 89 karma points
    Nov 16, 2017 @ 08:33
    Melvin Chew
    0

    Getting Media's URL from Umbraco's UID

    Hi,

    I'm using Umbraco V7.6 I'm quite new with umbraco and still experimenting this CMS.

    I'm trying to return a string of the media's URL in my XYZapicontroller class.

    in my XYZapicontroll class i added this following line,

                string media = (ctx.cmsPropertyDatas.Where(x2 => x2.propertytypeid == rwupload.id && x2.contentNodeId == x.nodeId).First<cmsPropertyData>().dataNtext == null) ? "none" : ctx.cmsPropertyDatas.Where(x2 => x2.propertytypeid == rwupload.id && x2.contentNodeId == x.nodeId).First<cmsPropertyData>().dataNtext;
    

    and i got back a string media of:

    <Media>umb://media/d5e0b425a5704c2d8117bae2c5447eab</Media>
    

    how do I convert this UID into the media's URL so that a mobile app that im developing will be able to play/view this media file?

    Thanks and Appreciate the help

    Melvin

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Nov 16, 2017 @ 09:10
    Dan Diplo
    3

    You should be able to convert using the Umbraco helper class, which is available in controllers derived from an Umbraco surface or API controller. Like this:

    IPublishedContent mediaItem = Umbraco.TypedContent(media);
    

    Where media string is your umb://media/ document.

  • Melvin Chew 10 posts 89 karma points
    Nov 21, 2017 @ 08:19
    Melvin Chew
    0

    Hi Dan

    I tried doing this.

                string media = (ctx.cmsPropertyDatas.Where(x2 => x2.propertytypeid == rwupload.id && x2.contentNodeId == x.nodeId).First<cmsPropertyData>().dataNtext == null) ? "none" : ctx.cmsPropertyDatas.Where(x2 => x2.propertytypeid == rwupload.id && x2.contentNodeId == x.nodeId).First<cmsPropertyData>().dataNtext;
                IPublishedContent mediaItem = Umbraco.TypedContent(media);
                string murl = mediaItem.UrlWithDomain();
    
    
                RARiskWatch rw = new RARiskWatch(id, title, body, murl);
    

    and my web Api returns this exception:

          <Error>
          <Message>An error has occurred.</Message>
          <ExceptionMessage>
          Object reference not set to an instance of an object.
          </ExceptionMessage>
          <ExceptionType>System.NullReferenceException</ExceptionType>
          <StackTrace>
          .......
          </StackTrace>
          </Error>
    

    Im not sure where im doing wrong. Thank you for your help!

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Nov 21, 2017 @ 08:55
    Dan Diplo
    0

    You probably need to do a few null checks. Also, it looks like your media string could be equal to the word "none" in some circumstances, which isn't a valid string to resolve.

    So if you go:

    IPublishedContent mediaItem = Umbraco.TypedContent(media);
    

    and media doesn't resolve to IPublishedContent then when you try and access mediaItem it can potentially be null. So when you call UrlWithDomain() on a null object, you're going to get a null reference exception.

    Can you debug and check what the value of "media" is?

  • Melvin Chew 10 posts 89 karma points
    Nov 21, 2017 @ 09:11
    Melvin Chew
    0

    Hi Dan,

    for the string media has a value of "umb://media/572ab5ce2ea54640bf124e922254c067"

    and the

    IPublishedContent mediaItem was null

    Melvin

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Nov 21, 2017 @ 11:28
    Dan Diplo
    1

    Ahhh, just thinking - as it's media you might need to use:

    IPublishedContent mediaItem = Umbraco.TypedMedia(media);
    

    Give that a go!

  • Melvin Chew 10 posts 89 karma points
    Nov 22, 2017 @ 06:15
    Melvin Chew
    0

    Hi Dan,

    I tried using TypeMedia but i still return a null exception.

    I was looking through the Documentations, TypeMedia(int ID) requires an input of integer typed. However in this case, my ID is a GUID typed which is 'umb://media/572ab5ce2ea54640bf124e922254c067'.

    1) Is there a way to return a media file using GUID of the media file??

    2) Or is there a way to return the ID of a media file using GUID of the media file?

    3) Or is there any helper class that has an input of GUID instead of Integer

    Thanks so much for your help

    Melvin

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Nov 22, 2017 @ 09:12
    Dan Diplo
    2

    I think there must be a bug or missing overload, then, as Umbraco.TypeContent() works for umb://document content types, so the same should be true for media. But clearly it doesn't work yet.

    A simple way around this is to first convert your string to a UDI. This is what Umbraco calls those URIs starting with umb://.

    So:

    string id = "umb://media/572ab5ce2ea54640bf124e922254c067";
    var udi = Udi.Parse(id);
    var media = Umbraco.TypedMedia(udi);
    

    This definitely works as I've checked in latest Umbraco. If you are using an earlier version and it doesn't then let me know.

    NB. I've flagged this as an issue on the Issue Tracker, so upvote: http://issues.umbraco.org/issue/U4-10706

  • David Amri 214 posts 740 karma points
    Oct 24, 2018 @ 14:16
    David Amri
    0

    Hi,

    I'm trying to convert a media udi string to media url but cannot use the "TypedMedia" helper.

    I know you submitted an issue on this. This was then fixed in version 7.11.0 according to the issue info. Now I'm using 7.11.1 and I still can't get the helper to work?

    I did try your solution above and that worked, but I don't understand why I cant use the "TypedMedia" helper. Any thoughts?

    I'm getting this data "umb://media/4378b79ac80e4db4930e4d651d7687f7" from the property value "imageItem.imageGalleryPicker"

    IPublishedContent mediaItem = Umbraco.TypedMedia(imageItem.imageGalleryPicker);
    

    Error message:

    Cannot implicitly convert type 'object' to 'Umbraco.Core.Models.IPublishedContent'. An explicit conversion exists (are you missing a cast?)

    Best regards /David

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Oct 24, 2018 @ 15:35
    Dan Diplo
    1

    Hi David,

    Yeah, I submitted the issue and I also submitted the PR that fixed it.

    I tested it when submitted and I've just checked on an Umbraco 7.12.3 instance I'm currently running and both these work:

    var media = Umbraco.TypedMedia("umb://media/572ab5ce2ea54640bf124e922254c067");
    
    var media = Umbraco.TypedMedia("572ab5ce2ea54640bf124e922254c067");
    

    Is imageItem.imageGalleryPicker a string?

  • John Bergman 483 posts 1132 karma points
    Nov 22, 2017 @ 06:39
    John Bergman
    100

    I think this thread has the answer you are looking for.

    https://our.umbraco.org/forum/developers/api-questions/67605-get-id-int-from-guid

  • Christian A 24 posts 98 karma points c-trib
    Nov 22, 2017 @ 08:09
    Christian A
    0

    I had this problem myself, using TypedContent did not work for media UDI's, however using TypedMedia solved my problem.

    TypedMedia constructor is not limited to Int ID's only (even though the documentation does not list the other implementations). It also support Udi, Guid and a string representation of the Id.

    This is directly from the UmbracoHelper implementation:

        public IPublishedContent TypedMedia(Udi id);
        public IPublishedContent TypedMedia(Guid id);
        public IPublishedContent TypedMedia(int id);
        public IPublishedContent TypedMedia(string id);
    
  • Melvin Chew 10 posts 89 karma points
    Nov 22, 2017 @ 08:52
    Melvin Chew
    0

    Hi Christian,

    I tried using

    public IPublishedContent TypedMedia(Guid id);
    

    but still got a null exception.

    Managed to solve my problem with the link that John shared:

                string media = (ctx.cmsPropertyDatas.Where(x2 => x2.propertytypeid == rwupload.id && x2.contentNodeId == x.nodeId).First<cmsPropertyData>().dataNtext == null) ? "none" : ctx.cmsPropertyDatas.Where(x2 => x2.propertytypeid == rwupload.id && x2.contentNodeId == x.nodeId).First<cmsPropertyData>().dataNtext;
                media = media.Remove(0, 12);
                IMedia content = UmbracoContext.Current.Application.Services.MediaService.GetById(Guid.ParseExact(media, "N"));
                IPublishedContent mediaItem = content == null ? null : Umbraco.TypedMedia(content.Id);
                string murl = mediaItem.Url();
    

    and i managed to get the url path of my media files.

    Thanks guys!

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Nov 22, 2017 @ 09:35
    Dan Diplo
    2

    Whilst that works it involves using the Media Service which hits the database and isn't that efficient.

    Check my alternative solution at: https://our.umbraco.org/forum/extending-umbraco-and-using-the-api/89243-getting-medias-url-from-umbracos-uid#comment-282293

  • Predrag Andrejević 10 posts 84 karma points
    Oct 11, 2019 @ 15:24
    Predrag Andrejević
    1

    I've been struggling with this problem and managed to solve it, so I thought why not share it. Here's my method for getting media url of Member image property:

    public static string GetImageUrl(IMember member, string imageProperty)
        {
            string url = "";
    
            if (member.GetValue(imageProperty) != null)
            {
                IPublishedContent image = Umbraco.Web.Composing.Current.UmbracoHelper.Media(member.GetValue(imageProperty));
                url = image.Url;
            }
    
            return url;
        }
    

    Hope it helps someone one day!

Please Sign in or register to post replies

Write your reply to:

Draft