If you want a more "API-ish" way to get the Content there is Content.GetContentFromVersion (used with property.VersionId) but I'm guessing your way is probably less DB hits ;)
The methods I've put in just return the media/content nodeId at the moment ... thinking was, if the dev had the nodeId, then they could get the doc/media themselves. But I do like the idea of getting a specific version of the content ... hmmm, definitely worth exploring! :-)
If you are using the uComponents package, then there's an XsltExtension called GetMediaIdByUrl(string url) ... don't be mislead by "XSLT" in the name... you can use it in Razor/C# code too.
IMediaService ms = Umbraco.Core.ApplicationContext.Current.Services.MediaService;
IMedia media = ms.GetMediaByPath("You url here");
I'm using 7.03 so the GetMediaByPath could be a new thing.
Get Media Id from URL
Is it possible to get the Id of a media item from a URL?
Hi Braydie,
There is some information on another topic about this: http://our.umbraco.org/forum/developers/api-questions/4284-getting-media-Id-from-path
This might be something we could add to uComponents in an upcoming release ... we'll look into that. :-)
Cheers, Lee.
Here's also a helper method I've used for the same purpose, been meaning to optimize and add to uComponents :)
GetMediaByPath
@Tom - Great minds think alike! Already added XsltExtension for uComponents 4.1.0: http://ucomponents.codeplex.com/SourceControl/changeset/f74faf78951d
Open to any improvements, suggestions.
Cheers, Lee
Oooh, nice one!
If you want a more "API-ish" way to get the Content there is Content.GetContentFromVersion (used with property.VersionId) but I'm guessing your way is probably less DB hits ;)
-Tom
The methods I've put in just return the media/content nodeId at the moment ... thinking was, if the dev had the nodeId, then they could get the doc/media themselves. But I do like the idea of getting a specific version of the content ... hmmm, definitely worth exploring! :-)
Cheers, Lee.
Yeah, the idea wasn't to get a specific version, just an API-ish way to get the Content from a PropertyID without knowing the node ID
But the way you're doing it now is probably less DB hits :)
Anyone? What to know this as well
Which version of Umbraco are you using?
Since v4.8 uQuery has offered a way to get media items by name: see documentation here.
If you are using the uComponents package, then there's an XsltExtension called
GetMediaIdByUrl(string url)
... don't be mislead by "XSLT" in the name... you can use it in Razor/C# code too.Cheers,
- Lee
I think I found it:
IMediaService ms = Umbraco.Core.ApplicationContext.Current.Services.MediaService; IMedia media = ms.GetMediaByPath("You url here");
I'm using 7.03 so the GetMediaByPath could be a new thing.The
GetMediaByPath
method would be expecting the ID path to the media item: e.g. "-1,1234,5678" (comma-separated list of the parent media items)Hey nojaf,
If you are using Umbraco 6 or newer you can use the method
GetMediaByPath(string mediaPath)
on the MediaServiceMediaService documentation
Dang, I was too slow!
The MediaService WILL hit the database for each request, make sure not to use this on the frontend.
The GetMediaIdByUrl method Lee mentions will likely have stopped working after we changed the way the /media/{number} folders were generated in v6.
I don't think there's currently a good way to get to the Id from the Url (without doing database lookups).
I see, it's not going to be used in the front so I think it will do just fine.
Thanks for the replies! h5yr!
is working on a reply...