Copied to clipboard

Flag this post as spam?

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


  • Craig O'Mahony 364 posts 918 karma points
    Jul 04, 2013 @ 23:29
    Craig O'Mahony
    0

    Get media properties by its name

    Hiya folks,

    I've asked this one before but I've just been unable to find anything that works.

    basically I have a page on which an integer is passed in on the query string, somewhere in the media content of my site I'm trying to find the media item based upon this name (ie 12341.png). Is it possible to search the media content knowing only a name of an item. Preferably I'd like to achieve this in a user control.

    this is driving me mental!!!

    Thanks,

    craig 

  • Jan Skovgaard 11264 posts 23594 karma points MVP 9x admin c-trib
    Jul 05, 2013 @ 01:38
    Jan Skovgaard
    0

    Hi Craig

    Do you have any code you're trying to get working that you can share with us? 

    I'm not that fluent in C# if that's what you're using but you should be able to use the umbraco.library:GetMedia() extension to fetch the media by the id from the querystring parameter.

    Also what version of Umbraco are you using?

    In the old API (In version before 4.10 I think) you should be able to do something like this

    Media file =newMedia(mediaId);
    string url = file.getProperty("umbracoFile").Value.ToString();

    In the above you then need to create a var mediaId with the value of the querystring.

    Looking forward to hearing from you.

    /Jan

  • Craig O'Mahony 364 posts 918 karma points
    Jul 05, 2013 @ 10:08
    Craig O'Mahony
    0

    Hi Jan,

    Thanks for your help. What I've currently got (that doesn't work!) is the following. This is code that I've copied from another forum to try to get it to work. What this 'should' do (I think) is to loop the entire media directory and return a list of whats in there but like I say this doesn't return anything: The nodeName variable is passed in as string (myimage.doc for instance). To be honest I think that this is far more complex than I need it to be and I can't get it to work anyway!

            string zMedia = "";

            XmlDocument xmlDocument = uQuery.GetPublishedXml(uQuery.UmbracoObjectType.Media);

            XPathNavigator xPathNavigator = xmlDocument.CreateNavigator();

            XPathNodeIterator xPathNodeIterator = xPathNavigator.Select("descendant::*[@nodeName='" + nodeName + "']");

     

            Media mediaItem;

            while (xPathNodeIterator.MoveNext())

            {

                mediaItem = uQuery.GetMedia(xPathNodeIterator.Current.Evaluate("string(@id)").ToString());

                if (mediaItem != null)

                {

                    //media.Add(mediaItem);

                    zMedia = zMedia + (mediaItem);

                }

            }

     

            return zMedia;

  • Dupeng 5 posts 25 karma points
    Aug 16, 2013 @ 12:28
    Dupeng
    0

    Do you have a solution yet?

  • Craig O'Mahony 364 posts 918 karma points
    Aug 16, 2013 @ 22:14
    Craig O'Mahony
    0

    Hiya Mate, 

    Yes I sorted it in the end. If you want the code I'll post it up for you early next week (I'm at work at the min)

    Craig

  • Emanuel Nilsson 3 posts 74 karma points
    Nov 22, 2016 @ 09:50
    Emanuel Nilsson
    0

    As an update to Jan Skovaards answer...

    In later versions of Umbraco you can use 'getPropertyValue' to get media properties.

    var media = Umbraco.Media(id);
    var byes = media.GetPropertyValue("umbracoBytes");
    
Please Sign in or register to post replies

Write your reply to:

Draft