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:
<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!
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.
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?
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
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.
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"
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);
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:
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,
and i got back a string media of:
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
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:
Where
media
string is yourumb://media/
document.Hi Dan
I tried doing this.
and my web Api returns this exception:
Im not sure where im doing wrong. Thank you for your help!
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:
and media doesn't resolve to IPublishedContent then when you try and access
mediaItem
it can potentially be null. So when you callUrlWithDomain()
on a null object, you're going to get a null reference exception.Can you debug and check what the value of "media" is?
Hi Dan,
for the string media has a value of "umb://media/572ab5ce2ea54640bf124e922254c067"
and the
IPublishedContent mediaItem was null
Melvin
Ahhh, just thinking - as it's media you might need to use:
Give that a go!
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
I think there must be a bug or missing overload, then, as
Umbraco.TypeContent()
works forumb://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:
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
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"
Error message:
Best regards /David
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:
Is
imageItem.imageGalleryPicker
a string?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
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:
Hi Christian,
I tried using
but still got a null exception.
Managed to solve my problem with the link that John shared:
and i managed to get the url path of my media files.
Thanks guys!
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
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:
Hope it helps someone one day!
is working on a reply...