Copied to clipboard

Flag this post as spam?

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


  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Mar 31, 2011 @ 15:40
    Jeroen Breuer
    0

    Speed if I use library.GetMedia recursive on top media node?

    Hello,

    Does anybody know if using umbraco.library.GetMedia on the topnode with recursive = true will be slow?

    Jeroen  

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Mar 31, 2011 @ 15:54
    Hendy Racher
    1

    Hi Jeroen,

    Do you want every media item ? or are you searching for particular ones ?

    btw, in uComponents there is

    using umbraco.cms.businesslogic.media;
    using uComponents.Core;

    List<Media> media = uQuery.GetMediaByXPath("/descendant::*");

    (The above would be a single DB hit)

    HTH,

    Hendy

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Mar 31, 2011 @ 16:04
    Jeroen Breuer
    0

    Hi Hendy,

    In MNTP you can use an xpath to select a start node. I want to do the same thing for DAMP, but I need to find out what is the best way to do an xpath on media, because media isn't stored in the xml cache (umbraco.config). I thought getting the top node with library.GetMedia would give all the media back as an XPathNodeIterator so I could use an xpath on that. Any better suggestions?

    Jeroen

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Mar 31, 2011 @ 17:46
    Jeroen Breuer
    0

    Hi Hendy,

    I just had a quick look at the source code of uQuery.GetPublishedXml and it seems that is what I am looking for. The only problem is the data which is retrieved isn't hierarchical. Could this be a problem for the xpath, because if so that would mean uQuery.GetMediaByXPath would also not work. Any idea's how to get all the media xml hierarchical? Btw uQuery totally rocks. Some methods in there are very powerfull!

    Jeroen

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Mar 31, 2011 @ 18:25
    Jeroen Breuer
    0

    Hello Hendy again :).

    Using library.GetMedia with recursive = true does return the media in a hierarchical structure, but unfortunately it doens't accept -1 as a node so I can't get the entire tree. However looking at the umbraco source code there is a method called Media.GetRootMedias() which returns all the top media nodes. This method is pretty heavy with a lot of DB calls, but after going a bit deeper it just calls this query:

    Select uniqueID from umbracoNode where nodeObjectType = @type And parentId = -1 order by sortOrder

    So I could call that query, loop through it myself. Call library.GetMedia recursive for each id (after changeing uniqueID in id) and merge all those results into 1 big xml file which is than hierarchical. Sounds like a good idea or did I miss something?

    Jeroen

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Mar 31, 2011 @ 18:41
    Jeroen Breuer
    0

    I'm gonna keep posting ;).

    If you look at the ToXml method on Content.cs in the umbraco source code you can see how it's done hierarchical. Unfortunately it uses more than 1 db call to do it. I'm not sure if it's possible with 1 db call. Wow this is a hard one :p. Still not sure what is the best way to get all the media xml.

    Jeroen

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Mar 31, 2011 @ 18:44
    Hendy Racher
    0

    Hi Jeroen,

    Please do keep posting :) would be really good to update uQuery as well to make the media and member xml data hierarachial.

    Hendy

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Apr 01, 2011 @ 12:52
    Jeroen Breuer
    0

    Hi Hendy,

    I once did a recursive query to get the parent of document type. The hierarchical xml is also build with a recursive method. Maybe I can build a recursive query to get all the xml hierarchical, but I'm not sure if this code can be executed as a query in C# because it was done in a stored procedure. If it works the xml can still be build with 1 DB call.

    Jeroen

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Apr 04, 2011 @ 11:57
    Jeroen Breuer
    0

    Hi Hendy,

    I'm having some problems with the recursive query idea, so for now I just used some of the API features since they are also fast enough. Here is my code to get the media in hierarchical xml:

    //Get the current user.
    User currentUser = User.GetCurrent();
    
    //Create an empty xmlDocument.
    XDocument xmlDocument = new XDocument(
            new XElement("Media")
        );
    
    if (currentUser.StartMediaId == -1)
    {
        //If the currentUser has no start node loop through all the top media nodes.
        using (IRecordsReader dr = SqlHelper.ExecuteReader("Select id from umbracoNode where nodeObjectType = @type And parentId = -1 order by sortOrder",
            SqlHelper.CreateParameter("@type", Media._objectType)))
        {
            while (dr.Read())
            {
                //Get the hierarchical xml from each topnode and add it to the xmlDocument.
                xmlDocument.Root.Add(XElement.Parse(library.GetMedia(dr.GetInt("id"), true).Current.OuterXml));
            }
        }
    }
    else
    {
        //Get the hierarchical xml from the user his start node and add it to the xmlDocument.
        xmlDocument.Root.Add(XElement.Parse(library.GetMedia(currentUser.StartMediaId, true).Current.OuterXml));
    }
    
    //Use an xPath on the xml.
    XPathNavigator xPathNavigator = xmlDocument.CreateNavigator();
    XPathNodeIterator xPathNodeIterator = xPathNavigator.Select(xPath);

    Since library.GetMedia is cached it will only be slow on the first call. After that it's fast!

    Jeroen

     

     

Please Sign in or register to post replies

Write your reply to:

Draft