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?
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!
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?
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.
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.
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!
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
Hi Jeroen,
Do you want every media item ? or are you searching for particular ones ?
btw, in uComponents there is
(The above would be a single DB hit)
HTH,
Hendy
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
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
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:
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
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
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
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
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:
Since library.GetMedia is cached it will only be slow on the first call. After that it's fast!
Jeroen
is working on a reply...