Copied to clipboard

Flag this post as spam?

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


  • Adrian Inman 35 posts 59 karma points
    Sep 20, 2011 @ 15:42
    Adrian Inman
    0

    Getting a node from its URL with C#

    I'm having real problems trying to get a node from its URL using the API - I want to be able to get at the node from its URL so I can return some JSON content, but I can't get anything sensible from umbraco.requestHandler.CreateXPathQuery - this returns 

    /root/* [ = "level1"]/* [ = "level2"]/* [ = "level3"]

    which even I can tell is practically useless. Even when manually replacing the [ with [@urlName I still don't get anything useful back.

    Help gratefully received.

    Thanks.

     

  • Gerty Engrie 120 posts 470 karma points c-trib
    Sep 20, 2011 @ 15:55
    Gerty Engrie
    0

    there's a function in the library which might help you out:

     

    umbraco.library.GetXmlDocumentByUrl(string url)

    Gerty

     

     

  • Adrian Inman 35 posts 59 karma points
    Sep 20, 2011 @ 16:02
    Adrian Inman
    0

    That just takes an external URL and returns it so you can do things like parse tweets etc.

    I want to take a URL for the umbraco site and retrieve its node from the NodeFactory ideally

  • Lee Kelleher 3938 posts 15122 karma points MVP 10x admin c-trib
    Sep 20, 2011 @ 16:02
    Lee Kelleher
    0

    Hi Adrian,

    The 'requestHandler.CreateXPathQuery' method should return you the appropriate XPath expression to locate the node.

    The output you gave is that exactly what you are getting back? As it doesn't look right at all.

    We use this approach in uComponents for an XSLT extension Url.GetNodeIdByUrl.

    There might be an alternative approach to this, but depends on where you are calling your code from? You could attempt to access HttpContext.Current.Items["pageID"]?

    Cheers, Lee.

  • Adrian Inman 35 posts 59 karma points
    Sep 20, 2011 @ 16:16
    Adrian Inman
    0

    I think this is deprecated in 4.7 as it was in the source for 4.6 and still shows in the binaries for 4.7, but I can't find it in the source for 4.7 on codeplex. I wonder if that's why it isn't returning anything useful anymore - it isn't used. It does beg the question, how does uComponents work when it calls it though.

  • Adrian Inman 35 posts 59 karma points
    Sep 20, 2011 @ 16:16
    Adrian Inman
    0

    I am actually trying to load some data in an ASP.Net Handler ashx which means that HttpContext.Current etc. won't hold anything useful at all. If there was an easier way, I'd be using it.

  • Lee Kelleher 3938 posts 15122 karma points MVP 10x admin c-trib
    Sep 20, 2011 @ 16:28
    Lee Kelleher
    0

    Not sure where you heard that 'CreateXPathQuery' had been depreciated? It is definitely still being used for Umbraco core to find the content node on page load.

    Thinking you might be right about the HttpContext ... how about moving your code/logic from a HttpHandler to an Umbraco /Base (RestExtension) method?

  • Adrian Inman 35 posts 59 karma points
    Sep 20, 2011 @ 17:13
    Adrian Inman
    0

    We found our problem! The underlying

    We're using this now:

    string xpathQuery = umbraco.requestHandler.CreateXPathQuery(context.Request.QueryString["path"], false).Replace("[ ", "[@urlName ").Replace("/root/*", "/root//*");

     

    Basically, the nodes we were looking at weren't directly under the root node - they were in a container which doesn't show up in the URL. The CreateXPathQuery method still doesn't return @urlName where necessary, so we still need to add this in with the replace above. The Umbraco 4.6 source has an initialization method which makes sure this is populated which we're not calling (they don't in uComponents either)

    Anyway, panic over. For now.

  • Dan Diplo 1505 posts 5905 karma points MVP 4x c-trib
    Sep 20, 2011 @ 20:47
    Dan Diplo
    4

    I realise you've solved this, but there's a method in uCompontents uQuery called GetNodeByUrl(string) that sounds like it is what you require. 

  • Martin Leduc 3 posts 92 karma points
    Jan 15, 2021 @ 20:39
Please Sign in or register to post replies

Write your reply to:

Draft