What's the quickest way of querying data via the API ?
Hi,
I'm currently building a custom admin section with associated trees to group content nodes of a specific docType - there are 1000s of them in a flat structure, hence it'll be useful to have additional trees each grouping by particular properties - alphabetically / by date etc...
I heard there was a super quick way of getting readonly access to content items though the api ? (I would normally query the XML, but need to return unpublished nodes too)
Currently I'm using : foreach (Content content in Document.getContentOfContentType(DocumentType.GetByAlias("docType"))) { ...}
For Readonly access you can use the nodeFactory. That queries the XML, so only published content. For the content maintenance package I've used the database dirtectly (it's the fastest way). You can download the sourcecode also so the query is in there.
For the time being the quickest way is to query the database yourself and then display the data. Please note I said the quickest, not the easiest :P
If you only want published nodes then you can go via the XML directly (GlobalSettings.ContentXML will return its relative path) or you can use nodeFactory as Richard suggested.
Since you did state you want the unpublished ones you have to use the Document API, which is very slow, that foreach will do a (n*2)+5 database queries, where n is the number of documents for that parent. I don't remember if Document.getContentOfContentType returns an Array or an IEnumerable<T>, but if it returns an array please make sure you store the method call result in a variable rather than directly using it in the foreach, otherwise you look will thrash your database!
Lastly you could check out a project of mine (the successor to LINQ to Umbraco) called the Umbraco Interaction Layer (http://uil.codeplex.com) which will create .NET class representations of the Umbraco DocTypes. It uses the Document API so it's no faster than above mentioned, just easier if you want it strongly typed.
Storing the method call in a variable before the the foreach makes a significant difference ! thanks. I'm thinking of creating an additional tree for unpublished content, and using the nodeFactory, although I suspect database queries will be the most likely final solution.
How about adding support for the most wanted queries in umbraco api before more start to develop their own hacks? Maybe as a part of 4.1 or as a seperate "umbracoqueryapi" project until linq to umbraco saves us in v5?
Jesper - I've got a task to see if I can fix up the performance of child nodes in 4.1 but I'm not sure if I will be able to get it in before the feature lock-down. I know the theory of what needs to be done, the practice, well that's a bit harder ;)
What's the quickest way of querying data via the API ?
Hi,
I'm currently building a custom admin section with associated trees to group content nodes of a specific docType - there are 1000s of them in a flat structure, hence it'll be useful to have additional trees each grouping by particular properties - alphabetically / by date etc...
I heard there was a super quick way of getting readonly access to content items though the api ? (I would normally query the XML, but need to return unpublished nodes too)
Currently I'm using : foreach (Content content in Document.getContentOfContentType(DocumentType.GetByAlias("docType"))) { ...}
Thanks in advance,
Hendy
Hi Hendy,
For Readonly access you can use the nodeFactory. That queries the XML, so only published content. For the content maintenance package I've used the database dirtectly (it's the fastest way). You can download the sourcecode also so the query is in there.
Hope it helps you,
Richard
For the time being the quickest way is to query the database yourself and then display the data. Please note I said the quickest, not the easiest :P
If you only want published nodes then you can go via the XML directly (GlobalSettings.ContentXML will return its relative path) or you can use nodeFactory as Richard suggested.
Since you did state you want the unpublished ones you have to use the Document API, which is very slow, that foreach will do a (n*2)+5 database queries, where n is the number of documents for that parent. I don't remember if Document.getContentOfContentType returns an Array or an IEnumerable<T>, but if it returns an array please make sure you store the method call result in a variable rather than directly using it in the foreach, otherwise you look will thrash your database!
Lastly you could check out a project of mine (the successor to LINQ to Umbraco) called the Umbraco Interaction Layer (http://uil.codeplex.com) which will create .NET class representations of the Umbraco DocTypes. It uses the Document API so it's no faster than above mentioned, just easier if you want it strongly typed.
Thanks for the tips :)
Storing the method call in a variable before the the foreach makes a significant difference ! thanks. I'm thinking of creating an additional tree for unpublished content, and using the nodeFactory, although I suspect database queries will be the most likely final solution.
I'd warn you about direct DB access for the following:
a) Here be dragons!
b) we may change the DB structure between releases with little-to-no documentation
How about adding support for the most wanted queries in umbraco api before more start to develop their own hacks? Maybe as a part of 4.1 or as a seperate "umbracoqueryapi" project until linq to umbraco saves us in v5?
/Jesper
Jesper - I've got a task to see if I can fix up the performance of child nodes in 4.1 but I'm not sure if I will be able to get it in before the feature lock-down. I know the theory of what needs to be done, the practice, well that's a bit harder ;)
is working on a reply...