Copied to clipboard

Flag this post as spam?

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


  • bayshield 50 posts 65 karma points
    Jul 27, 2009 @ 14:56
    bayshield
    0

    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

  • Petr Snobelt 923 posts 1535 karma points
    Jul 27, 2009 @ 16:20
    Petr Snobelt
    0

    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?

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jul 28, 2009 @ 00:35
    Aaron Powell
    1

    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.

  • Petr Snobelt 923 posts 1535 karma points
    Jul 28, 2009 @ 09:19
    Petr Snobelt
    0

    @slace: Thanks for info.

    BTW: After linq4umbraco will be released, what will be faster?

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jul 28, 2009 @ 11:20
    Aaron Powell
    0

    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.

  • Petr Snobelt 923 posts 1535 karma points
    Jul 28, 2009 @ 11:46
    Petr Snobelt
    0

    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

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jul 28, 2009 @ 23:41
    Aaron Powell
    1

    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

  • Rich Green 2246 posts 4008 karma points
    Jun 14, 2010 @ 14:25
    Rich Green
    0

    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

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jun 14, 2010 @ 14:49
    Aaron Powell
    0

    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.

  • Rich Green 2246 posts 4008 karma points
    Jun 14, 2010 @ 15:10
    Rich Green
    0

    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?

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Jun 14, 2010 @ 15:19
    Aaron Powell
    1
    var node = umbraco.library.GetXmlNodeByXPath("//node[@nodeName='Something here' and contains(@path, '1234')]");

    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

  • Rich Green 2246 posts 4008 karma points
    Jun 14, 2010 @ 16:23
    Rich Green
    0

    Thanks slace.

    Code I ended up using here for future reference

    var node = umbraco.library.GetXmlNodeByXPath("//node[@id='1050']/node[@nodeName='some node']");
    
               if (node.Count>0) {
                   //Node exists
               }
               else{
                   //Node does not exist
               }

     

     

     

  • Rich Green 2246 posts 4008 karma points
    Jun 14, 2010 @ 17:03
    Rich Green
    0

    Hmm, actually this returns a System.Xml.DocumentXPathNavigator

    So how do I get the node @id if the node does exist?

     

Please Sign in or register to post replies

Write your reply to:

Draft