I am trying to figure out the most efficient way of determining if a document exists using the API. I have a site with approximately 2000 pages (all the same doc type) that have a unique property.
So far my research is pointing towards using an XPath query. Is this the best way or is there an API method that I am not finding?
Yes, XPath will be the best way unless you want to write the SQL query yourself. The Document API is far from being optimised (you'd thrash your database like crazy) and I don't think that Node is really any faster.
LINQ to Umbraco is only shipping with a NodeDataProvider, which interacts with the XML cache (umbraco.config). It doesn't look like I'll have time to write a Database version, it's something I may look atas a package if I get time/ can work out a good way to achieve it. Otherwise it's something that'll be left until v5
It depends on whether it's safe for you to assume that the node is published. If so you can do it with pure XPath, not hard at all.
If you want to cover all bases and check both published and unpublished content you'll have to query the database. I'd write my own SQL to query the umbracoNode table and do a check on the name where the path contains your required ID. This way it'll be fast, and only a single DB hit for the check.
var node = umbraco.library.GetXmlNodeByXPath("//node[@id='1050']/node[@nodeName='some node']");
if (node.Count>0) {
//Node exists
}
else{
//Node does not exist
}
How to check if a document exists via API
Hi,
I am trying to figure out the most efficient way of determining if a document exists using the API. I have a site with approximately 2000 pages (all the same doc type) that have a unique property.
So far my research is pointing towards using an XPath query. Is this the best way or is there an API method that I am not finding?
Cheers
Aaron
I'm also interesting in most effecient way.
Now I use xpath (like http://www.stonehenge.org/blog/Finding-Umbraco-nodes-by-name.html ). Is there better way?
Yes, XPath will be the best way unless you want to write the SQL query yourself. The Document API is far from being optimised (you'd thrash your database like crazy) and I don't think that Node is really any faster.
@slace: Thanks for info.
BTW: After linq4umbraco will be released, what will be faster?
Compared to what? LINQ2Umbraco is a generalized concept and can be implemented as optimal or poorly as you want.
Using the NodeDataProvider you should have performance around the same as directly querying the XML.
Compare Linq2Umbraco with NodeDataProvide. I know I can use linq against xml or db, I'm interesting about Linq2Umbraco.
If I will use linq2Umbraco will be querying database or xml cache (without unpublished content)?
Maybe is too early to ask questions about Linq2Umbraco?
And another small question is possible to use umbraco.library getxmlnodebyxpath in unittests? If yes how?
Thank you
LINQ to Umbraco is only shipping with a NodeDataProvider, which interacts with the XML cache (umbraco.config). It doesn't look like I'll have time to write a Database version, it's something I may look atas a package if I get time/ can work out a good way to achieve it. Otherwise it's something that'll be left until v5
I need to do this however the link that Peter provided is now dead.
Has anyone got an example using the API, or using Hendy's API helper class?
Basically I need to see if a node exists by name under a specific node, if not I need to create it.
Many thanks
Rich
It depends on whether it's safe for you to assume that the node is published. If so you can do it with pure XPath, not hard at all.
If you want to cover all bases and check both published and unpublished content you'll have to query the database. I'd write my own SQL to query the umbracoNode table and do a check on the name where the path contains your required ID. This way it'll be fast, and only a single DB hit for the check.
Thanks slace, it's safe for us to assume the node will be published.
Could anyone point me in the direction of an Xpath example using the API?
I'm not sure if it throws an exception if the node doesn't exist, but that XPath should be fine.
Yes that's right people, I just answered a question with XPath :P
Thanks slace.
Code I ended up using here for future reference
Hmm, actually this returns a System.Xml.DocumentXPathNavigator
So how do I get the node @id if the node does exist?
is working on a reply...