Copied to clipboard

Flag this post as spam?

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


  • kukuwka 75 posts 96 karma points
    Aug 04, 2010 @ 10:00
    kukuwka
    0

    Get media by name

    Hi,

    How can I get media(folder) by name.

    Thanks

  • Matt Brailsford 4124 posts 22215 karma points MVP 9x c-trib
    Aug 04, 2010 @ 10:07
    Matt Brailsford
    0

    Hi Kukuwka,

    There isn't really possible out of the box, but you could write a helper method to do it.

    Take a look at Hendys helper methods for umbraco

    http://blog.hendyracher.co.uk/umbraco-helper-class/

    He has one for GetNodesByName which you could modify to work with media items.

    Many thanks

    Matt

  • kukuwka 75 posts 96 karma points
    Aug 04, 2010 @ 11:37
    kukuwka
    0


    I want get media name from Section Media ,but not from umbraco.config.

    Something like: Instead function "GetMedia(int itemid)" to use "GetMedia(string name)" .


    Is there such a possibility?

    Thanks

  • Chad Rosenthal 272 posts 474 karma points
    Dec 16, 2010 @ 21:50
    Chad Rosenthal
    2

    Don't know if you figured this out, but I modified some of the code in the uComponents library to do this.

                       public static List<Media> GetMediaByName(string nodeName)
            {
                List<Media> media = new List<Media>();
    
                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);
                    }
                }
    
                return media;
            }

    Hope this helps. Better late than never.

    -C

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Dec 16, 2010 @ 22:59
    Hendy Racher
    2

    Hi Chad,

    Yeah, that method should be in the API :) have just added:

    public static List<Media> GetMediaByName(string name)
    {
        return uQuery.GetMediaByXPath("descendant::*[@nodeName='" + name + "']");
    }

    Cheers,

    Hendy

Please Sign in or register to post replies

Write your reply to:

Draft