Copied to clipboard

Flag this post as spam?

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


  • suzyb 474 posts 932 karma points
    Sep 13, 2011 @ 15:12
    suzyb
    0

    GetNodeByXpath equivalent for documents

    I want to be able to find a page using one of it's properties.  I can do so using

    Node.GetNodeByXpath("//ConsultantProfile[userId = '" + userID + "']")

    however that only works for published pages and I need to be able to find the page if it is published or not.

    Is there some way of selecting a document based on the value of it's property.  Or will I have to loop through all the documents of type ConsultantProfile, which could be quite intensive as there will be 500+.

  • Richard 146 posts 168 karma points
    Sep 14, 2011 @ 13:09
    Richard
    0

    A "Node" is a published page, to get unpublished content you will need to use "Document", check the Wiki Difference between a Node and a Document. Then have a look at Enumerating documents for get documents below a particular node.

    Richard

  • suzyb 474 posts 932 karma points
    Sep 14, 2011 @ 13:17
    suzyb
    0

    So the only way is to loop the documents then :/

  • Richard 146 posts 168 karma points
    Sep 14, 2011 @ 13:34
    Richard
    0

    Looking at the source code for Umbraco 4.0.4, there is no equivalent to "GetNodeByXpath". There is the method, on the Document class:

    public static Document[] GetChildrenForTree(int NodeId)

    which says it is the optimized version, to recursively get documents below NodeId.

    Richard

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Sep 14, 2011 @ 13:37
    Hendy Racher
    0

    Hi Suzyb,

    FYI, there are some uQuery methods to help enumerate documents, so you could something like:

    Document doc = GetDocumentsByType("consultantProfile")
    .Where(x => x.GetProperty("userID") == userID)
    .FirstOrDefault();

    or if you know that the document you're looking for has a specific parent:

    Document doc = uQuery.GetDocument(parentID)
    .GetChildDocuments()
    .Where(x => x.GetProperty("userID") == userID)
    .FirstOrDefault();

    HTH,

    Hendy

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Sep 14, 2011 @ 22:10
    Hendy Racher
    0

    if iterating though the Document objects is too slow, how about querying the Lucene index with Examine ?

  • suzyb 474 posts 932 karma points
    Sep 21, 2011 @ 20:53
    suzyb
    0

    Ended up just looping through the documents.  The code should only be run very occasionally so hopefully if it's slow it wont be that big an issue.

Please Sign in or register to post replies

Write your reply to:

Draft