Copied to clipboard

Flag this post as spam?

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


  • Razvan 36 posts 67 karma points
    Nov 25, 2010 @ 12:19
    Razvan
    0

    Get Node ID from URL

    Hello,

      Does anyone know how can I get the node ID from an url using C# ? 

    Thanks

  • elspiko 133 posts 302 karma points
    Nov 25, 2010 @ 12:21
  • Razvan 36 posts 67 karma points
    Nov 25, 2010 @ 12:23
    Razvan
    0

    ohh thanks, was searching for this all day :)

    Umbraco Comunity is amazing!

  • Anders Brohäll 295 posts 561 karma points c-trib
    Feb 13, 2013 @ 09:37
    Anders Brohäll
    0

    The url above is down. 
    Can any of you repost the solution?

    Thanks! 

  • Hendy Racher 861 posts 3844 karma points MVP 2x admin c-trib
    Feb 13, 2013 @ 10:33
    Hendy Racher
    0

    Hi Anders,

    Not sure if this was the solution proposed, but there's a method in uQuery.GetNodeByUrl() which is in the umbraco namespace.

    HTH,

    Hendy

  • Anders Brohäll 295 posts 561 karma points c-trib
    Feb 13, 2013 @ 10:37
    Anders Brohäll
    0

    Isn't uQuery a part of uComponents?
    The version i'm on ATM for this site  is 4.7, and i can't seem to find the uQuery namespace in umbraco.

    And i dont want to install uComponents. 
    There has to be some other way? 

  • Hendy Racher 861 posts 3844 karma points MVP 2x admin c-trib
    Feb 13, 2013 @ 10:45
    Hendy Racher
    0

    Hi Anders,

    uQuery was moved into core, so should be available in the umbraco namespace (from the umbraco.dll assembly)

  • Anders Brohäll 295 posts 561 karma points c-trib
    Feb 13, 2013 @ 10:46
    Anders Brohäll
    0

    But that was in v4.8, right?

  • Hendy Racher 861 posts 3844 karma points MVP 2x admin c-trib
    Feb 13, 2013 @ 10:55
    Hendy Racher
    0

    arh yes, you're correct, from v4.8 onwards.

  • Lee Kelleher 3945 posts 15163 karma points MVP 10x admin c-trib
    Feb 13, 2013 @ 11:25
    Lee Kelleher
    1

    @Anders - no love for uComponents? :-(

    The original link is still available via archive.org: http://web.archive.org/web/20120123141406/http://www.grail-technology.co.uk/blog/get-node-by-url/

    Alternatively, feel free to check the uQuery source (either uComponents or Umbraco core) and rip out the "GetNodeByUrl" code. :-)

    Cheers, Lee.

  • Anders Brohäll 295 posts 561 karma points c-trib
    Feb 13, 2013 @ 11:27
    Anders Brohäll
    0

    @Lee, of course! But since it's quite big i don't want to install it for a single method. 

    Thanks for the link!
    : )

  • elspiko 133 posts 302 karma points
    Feb 13, 2013 @ 11:28
    elspiko
    1

    Apologise for the link being down, had major issues with hosting provider so left and given I hadn't had any traffic to my site in forever didn't rush to bring it back up.

    @Lee thanks bailing me out :)

  • Lee Kelleher 3945 posts 15163 karma points MVP 10x admin c-trib
    Feb 13, 2013 @ 11:29
    Lee Kelleher
    1

    No worries - I totally appreciate that it's a big library, and overkill for what you want to achieve!

    If you've got your own code library, then do feel free to rip out any parts of uComponents code - it's all MIT licensed. :-)

    Cheers, Lee.

  • Anders Brohäll 295 posts 561 karma points c-trib
    Feb 13, 2013 @ 11:29
    Anders Brohäll
    0

    Cheers

  • elspiko 133 posts 302 karma points
    Feb 14, 2013 @ 10:10
    elspiko
    1

    Just going to paste the code in here for future reference:

    public int GetNodeIdFromUrl(string url)
    {
        int nodeId = -1;
        string xpathUrl = url == "/" ? "home" : url;
        string xpath = String.Format("//node[@urlName='{0}']", xpathUrl);
        var node = GetXmlNodeByXPath(xpath);
        if (node != null && node.Count > 0)
        {
            while (node.MoveNext())
            {
                var currentnode = node.Current.GetAttribute("id", "");
                int.TryParse(currentnode, out nodeId);
            }
        }
    
        return nodeId;
    }
    
    private XPathNodeIterator GetXmlNodeByXPath(string xpathQuery)
    {
        XPathNavigator xp = content.Instance.XmlContent.CreateNavigator();
        if (xp != null) return xp.Select(xpathQuery);
        return null;
    }
    

     

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies