Copied to clipboard

Flag this post as spam?

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


  • Matt Brailsford 4125 posts 22224 karma points MVP 9x c-trib
    Sep 29, 2010 @ 13:26
    Matt Brailsford
    0

    Getting the referring pages NodeID?

    Hey Guys,

    Anybody got any good suggestions on how to get the reffering pages NodeID? (ie, when you navigate to a page, I want to know the ID of the page you've just come from)

    Cheers

    Matt

  • Tom Maton 387 posts 660 karma points
    Sep 29, 2010 @ 13:33
    Tom Maton
    0

    Hi Matt

    Off the top of my head cant you use the GetXmlDocumentByUrl(URL) and get the URL variable using the RequestServerVariables(referrer)

    Then query the XML returned?

    Thanks

    Tom

  • Matt Brailsford 4125 posts 22224 karma points MVP 9x c-trib
    Sep 29, 2010 @ 13:36
    Matt Brailsford
    0

    Hi Tom,

    Wouldn't GetXmlDocumentByUrl try to load the refering page as XML? rather than get the xml for the refering page?

    Matt

  • Sebastiaan Janssen 5061 posts 15544 karma points MVP admin hq
    Sep 29, 2010 @ 13:43
    Sebastiaan Janssen
    0

    Yeah, that would just be like a screenscrape of the page. 

    I guess if I wanted to do this I would do something like set a cookie with the current nodeId on each page and read it before I change it to the new nodeId.

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Sep 29, 2010 @ 13:45
    Aaron Powell
    0

    Do you mean you want to know if the ID of /Home if the user has gone /Home -> /Home/About-Us ?

    You'd need to have some kind of reverse-NiceUrl using the referrer on the HttpRequest.UrlReferrer property. I'd probably do this all in .NET, I think trying to do it in XSLT would be a bitch :P

  • Dave Forster 28 posts 74 karma points
    Sep 29, 2010 @ 13:46
    Dave Forster
    0

    How about in page load...

    int previousNodeId = 0;

    int.TryParse(Session["last_page"], out previousNodeId);

    Session["last_page"] = Node.GetCurrent().Id

  • Matt Brailsford 4125 posts 22224 karma points MVP 9x c-trib
    Sep 29, 2010 @ 13:49
    Matt Brailsford
    1

    @slace that might be another nice one for uComponent extension library?

    @sebastiaan / @dave, not bad suggestions. Might do the trick.

    Matt

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Sep 29, 2010 @ 13:54
    Aaron Powell
    1

    You'd have to speak to the uComponents guys, I'm not on that project :P

    But this should get you started:

    var referrer = Request.UrlReferrer.AbsoluteUrl;
    
    var parts = referrer.Split('/');
    var xpath = "/";
    foreach(var part in parts) 
    {
        xpath += "/*[@nodeName='" + part + "]";
    }
    xpath += "/@id";

    That'll get you an XPath statement you can throw at a method in umbraco.library to execute and get your ID.

    Note - I just wrote this in notepad so it's not tested, and it only supports directory URLs, you'll have to strip the .aspx from the end if you have to

  • Tom Maton 387 posts 660 karma points
    Sep 29, 2010 @ 13:55
    Tom Maton
    0

    Hi Matt,

    Couldn't you assign the GetXmlDocumentByUrl to a variable and then query that variable?

    eg

    <xsl:value select="GetXmlDocumentByUrl(URL)/@id"/>

    Or something similar

    Tom

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Sep 29, 2010 @ 13:55
    Douglas Robar
    3

    You might be able to get clever with umbraco.library:GetXmlNodeByXpath()

    http://our.umbraco.org/wiki/reference/umbracolibrary/getxmlnodebyxpath

    The idea would be to grab the referrer from the request headers, and then split the url into an xpath statement. Should be relatively straightforward. You then get the umbraco node of the referrer, including its ID.

    Let us know how you finally resolve this, it's an interesting question.

    cheers,
    doug.

  • Lee Kelleher 4026 posts 15837 karma points MVP 13x admin c-trib
    Sep 29, 2010 @ 13:59
    Lee Kelleher
    0

    Hey Matt,

    There used to be a stats module in the Umbraco core (you'd enable the "umbracoEnableStat" setting in the Web.config), but that seems to have been removed in v4.5! (not sure why?)

    One suggestion could be to use "umbraco.requestHandler.CreateXPathQuery" to take the URL ... then use that XPath to get the node info?

    Cheers, Lee.

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Sep 29, 2010 @ 14:02
    Aaron Powell
    0

    @Lee - it was removed in v4 (or maybe even v3), there was a few pieces of legacy code that lay around that we removed for 4.5. The code wasn't doing anything.

  • Jonas Eriksson 930 posts 1825 karma points
    Sep 29, 2010 @ 14:05
    Jonas Eriksson
    2

    works for me:

        public static int GetReferringNodeId()
        {
            string url = System.Web.HttpContext.Current.Request.UrlReferrer.AbsolutePath;
            string realUrlXPath = requestHandler.CreateXPathQuery(url, true);
            System.Xml.XmlNode urlNode = content.Instance.XmlContent.SelectSingleNode(realUrlXPath);
            int nodeId=-1;
            if (urlNode != null)
                nodeId = int.Parse(urlNode.Attributes.GetNamedItem("id").Value);
    
            return nodeId;
        }

    ref: umbraco » presentation » umbracoBase » requestHandler.cs

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Sep 29, 2010 @ 14:07
    Douglas Robar
    0

    FWIW, the 'umbracoEnableStat' entry was from the very first 'pro' product from Umbraco... Umbraco Stats. It came out around the end of v2 and was shelved during the life of v3. You can google more about it you're interested in the history.

    In any event, the entry didn't do anything unless you had the product installed.

    cheers,
    doug. (they guy with a mind like a steel trap... nothing gets in... no, wait, out! ... oh, nevermind)

  • Lee Kelleher 4026 posts 15837 karma points MVP 13x admin c-trib
    Sep 29, 2010 @ 14:19
    Lee Kelleher
    0

    I'm sure Stats was part of Umbraco v3 core.  I remember playing around with it - trying to figure out how I could use it. But never used it on a "real world" project!

    So... begs the question to why is "umbracoEnableStat" still in the appSettings? (not that it really matter, more curious)

    Cheers, Lee.

  • Aaron Powell 1708 posts 3046 karma points c-trib
    Sep 29, 2010 @ 14:23
    Aaron Powell
    1

    Cuz those lazy sods on the core haven't removed it :P

  • Matt Brailsford 4125 posts 22224 karma points MVP 9x c-trib
    Sep 29, 2010 @ 14:31
    Matt Brailsford
    0

    @Jonas - Niiiice, I'll go give that a try =)

    Matt

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Sep 29, 2010 @ 14:34
    Douglas Robar
    1

    @Lee - you're partly right that it was partly in the core and made some tables and populated them. That was the part with the performance problem and so it shouldn't be used.

    This is an ancient forum post that has all the relevant info (along with some not-so-relevant-info) if you're up for some ancient history and why you should probably never enable the stats feature. http://forum.umbraco.org/yaf_postst4616_umbracoStats-changing-to-umbracoReports.aspx

    cheers,
    doug.

  • Matt Brailsford 4125 posts 22224 karma points MVP 9x c-trib
    Sep 29, 2010 @ 15:18
    Matt Brailsford
    0

    @Jonas - Worked like a charm. Nice snippet.

    Matt

  • Jonas Eriksson 930 posts 1825 karma points
    Sep 29, 2010 @ 15:31
    Jonas Eriksson
    1

    :-) I was looking for something similar the other day (node id by url), and following the ideas of Slace and Lee here, pieces got together nicely now. Perhaps this could be of use in uComponents?

    public static int GetNodeIdByUrl(string url)
       
    {
            string realUrlXPath = requestHandler.CreateXPathQuery(url, true);
           
    System.Xml.XmlNode urlNode = content.Instance.XmlContent.SelectSingleNode(realUrlXPath);
           
    int nodeId=-1;
           
    if (urlNode != null)
                nodeId
    = int.Parse(urlNode.Attributes.GetNamedItem("id").Value);

           
    return nodeId;
       
    }

  • Matt Brailsford 4125 posts 22224 karma points MVP 9x c-trib
    Sep 29, 2010 @ 15:35
    Matt Brailsford
    0

    @jonas - Definatley. As I'm on the team now (@Lee finaly let me in ;) I can add it to the library if you'd like. Or you can ask @Lee/@Shanon to add you.

    Matt

  • Lee Kelleher 4026 posts 15837 karma points MVP 13x admin c-trib
    Sep 29, 2010 @ 15:36
    Lee Kelleher
    1

    Hey Jonas,

    One step ahead... already added it to uComponents v2 (release in the next few weeks).

    I'll make sure to give you credit in the XML comments! ;-)

    Cheers, Lee.

  • Jonas Eriksson 930 posts 1825 karma points
    Sep 29, 2010 @ 15:42
    Jonas Eriksson
    0

    Ah, cool, you're fast as lightning!

    Cheers!

  • Matt Stueve 30 posts 49 karma points
    Oct 06, 2010 @ 03:39
    Matt Stueve
    0

    I'm trying to do something similar here but am running into an exception using the supplied code.  If I debug through, I see the following values being set:

    url: /the-sporting-life/event-calendar.aspx

    realUrlXPath: /root/* [ = \"the-sporting-life\"]/* [ = \"event-calendar\"]

    And then I get an exception on this line:

    System.Xml.XmlNode urlNode = umbraco.content.Instance.XmlContent.SelectSingleNode(realUrlXPath);

    The exception is: "System.Xml.XPath.XPathException: Expression must evaluate to a node-set."  Any ideas what I'm doing wrong?

    Thanks!

    Matt

  • Lee Kelleher 4026 posts 15837 karma points MVP 13x admin c-trib
    Oct 06, 2010 @ 09:22
    Lee Kelleher
    0

    Hi Matt,

    That's weird, the XPath expression is wrong - the predicates are missing the @urlName attribute!

    How are you getting the "realUrlXPath" value? Is it by using the "CreateXPathQuery" method? or another way?

    Cheers, Lee.

  • Jonas Eriksson 930 posts 1825 karma points
    Oct 06, 2010 @ 10:23
    Jonas Eriksson
    1

    Ah, remove the ".aspx":

        public static int GetReferringNodeId()
       
    {
           
    string url = System.Web.HttpContext.Current.Request.UrlReferrer.AbsolutePath;
    url = url.Replace(".aspx", string.Empty);
           
    string realUrlXPath = requestHandler.CreateXPathQuery(url, true);
           
    System.Xml.XmlNode urlNode = content.Instance.XmlContent.SelectSingleNode(realUrlXPath);
           
    int nodeId=-1;
           
    if (urlNode != null)
                nodeId
    = int.Parse(urlNode.Attributes.GetNamedItem("id").Value);

           
    return nodeId;
       
    }
  • Matt Stueve 30 posts 49 karma points
    Oct 06, 2010 @ 18:15
    Matt Stueve
    0

    Thanks, Jonas, that fixed the exception.  For some reason the SelectSingleNode call doesn't find my page but at least it's no longer throwing an error.  I'll see if I can figure out why it can't locate the referring page in the content.Instance.XmlContent.

  • Peter Duncanson 430 posts 1360 karma points c-trib
    Mar 19, 2012 @ 16:45
    Peter Duncanson
    0

    Just tried using this but it does not like me passing through referrer as it has the query string attached. Need a way of stripping that off but thats pushing a lot of code into my XSLT which I think should just be in the C#...any ideas?

  • Lee Kelleher 4026 posts 15837 karma points MVP 13x admin c-trib
    Mar 19, 2012 @ 16:54
    Lee Kelleher
    1

    @Pete, Could go for something like this...

    var url = System.Web.HttpContext.Current.Request.UrlReferrer.PathAndQuery.Replace(System.Web.HttpContext.Current.Request.UrlReferrer.Query)

    The "Path" property isn't available on the UrlReferrer, (not sure why).

    Cheers, Lee.

  • Peter Duncanson 430 posts 1360 karma points c-trib
    Mar 19, 2012 @ 16:58
    Peter Duncanson
    0

    Hacked it in XSLT the end, just wanted to know if the referring page was from a particular page on the site:

    <xsl:variable name="SearchPageNodeId" select="1134" />
    <xsl:variable name="IsFromSearchPage" select="contains( umbraco.library:RequestServerVariables('HTTP_REFERER'), umbraco.library:NiceUrl( $SearchPageNodeId ) )" />

    That did the trick and enough to get me running. I use this to work out if I should inject a "history.back()" click event for a "Back to search" button (god I hate these...)

    Cheers

    Pete

     

  • 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