Copied to clipboard

Flag this post as spam?

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


  • Braydie 148 posts 346 karma points
    Sep 03, 2012 @ 17:12
    Braydie
    0

    Get Media Id from URL

    Is it possible to get the Id of a media item from a URL?

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Sep 03, 2012 @ 18:09
    Lee Kelleher
    1

    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.

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Sep 06, 2012 @ 17:46
    Tom Fulton
    1

    Here's also a helper method I've used for the same purpose, been meaning to optimize and add to uComponents :)

    GetMediaByPath

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Sep 06, 2012 @ 17:53
    Lee Kelleher
    0

    @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

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Sep 06, 2012 @ 18:04
    Tom Fulton
    0

    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

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Sep 06, 2012 @ 18:08
    Lee Kelleher
    0

    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.

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Sep 06, 2012 @ 18:11
    Tom Fulton
    1

    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

    var property = new umbraco.cms.businesslogic.property.Property(1234);
    var content = Content.GetContentFromVersion(property.VersionId);

    But the way you're doing it now is probably less DB hits :)

  • nojaf 91 posts 300 karma points
    Feb 06, 2014 @ 11:48
    nojaf
    0

    Anyone? What to know this as well

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Feb 06, 2014 @ 11:57
    Lee Kelleher
    0

    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

  • nojaf 91 posts 300 karma points
    Feb 06, 2014 @ 11:57
    nojaf
    0

    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.

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Feb 06, 2014 @ 11:59
    Lee Kelleher
    0

    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)

  • Braydie 148 posts 346 karma points
    Feb 06, 2014 @ 12:00
    Braydie
    0

    Hey nojaf,

    If you are using Umbraco 6 or newer you can use the method GetMediaByPath(string mediaPath) on the MediaService

    MediaService documentation

  • Braydie 148 posts 346 karma points
    Feb 06, 2014 @ 12:01
    Braydie
    0

    Dang, I was too slow!

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Feb 06, 2014 @ 12:01
    Sebastiaan Janssen
    2

    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).

  • nojaf 91 posts 300 karma points
    Feb 06, 2014 @ 13:27
    nojaf
    0

    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!

Please Sign in or register to post replies

Write your reply to:

Draft