Copied to clipboard

Flag this post as spam?

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


  • MOthman 27 posts 89 karma points
    Apr 05, 2012 @ 13:52
    MOthman
    0

    Response.Redirect to next preceding sibling with umbraco.NodeFactory GetNodeByXpath from .net Usercontrol

    I want to redirect customers from one page to its first preceding sibling from .NET usercontrol. This is my code but I get exception "$currentPage/preceding-siblings::* [@isDoc][1] **has an invalid token**."

     

        Node sibling = Node.GetNodeByXpath(@"$currentPage/preceding-siblings::* [@isDoc][1]");

        if (sibling != null)

            Response.Redirect(umbraco.library.NiceUrl(sibling.Id));

     

    As of course currentPage is not a defined parameter in .net usercontrol, I thought about removing it, so, I wrote this

     

        Node sibling = Node.GetNodeByXpath(@"/preceding-siblings::* [@isDoc][1]");

        if (sibling != null)

            Response.Redirect(umbraco.library.NiceUrl(sibling.Id));

     

    I tried more ways, nothing works and I don't want to go with too long solutions. I can't figure out what is wrong in my code, any ideas! is there any other way to do that. Note, I must do it from within .net usercontrol not xslt. Thanks in advace

  • MOthman 27 posts 89 karma points
    Apr 05, 2012 @ 14:20
    MOthman
    0

    EDIT: I am sorry guys, this is not the right solution, please see Lee's next solution and my little tweeks after his.

    Ah, I guess I was blind... first of course there is no $currentPage and instead I should use a **dot .** and, preceding-siblings should be preceding-sibling without **s** at the end.

     

    So my working code is:

    ******* EDIT: This wasn't the right solution *********

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Apr 05, 2012 @ 14:26
    Lee Kelleher
    0

    Hi MOthman,

    Unfortunately the "Node.GetNodeByXpath" method has no context of the "$currentPage" parameter ... (that only works within XSLT).

    Instead, try something like this...

    var currentPage = Node.GetCurrent();
    var sibling = Node.GetNodeByXpath(string.Concat("descendant::*[@isDoc and @id = ", currentPage.Id, "]/preceding-siblings::* [@isDoc][1]"));
    if (sibling != null)
    {
        Response.Redirect(umbraco.library.NiceUrl(sibling.Id));
    }

    You'll get the current node/page, then use its Id to create the approprate XPath expression to get the first preceding-sibling.

    Cheers, Lee.

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Apr 05, 2012 @ 14:32
    Lee Kelleher
    0

    MOthman, you might want to double-check your solution... the XPath used in the "Node.GetNodeByXpath" method is applied to the entire content node tree (e.g. the XML cache) ... which means that if you use "." (dot) - which implies the "current" node, e.g. the root node, then using the "//" (double-slash) which means get ALL descendant nodes.  There is still no context to the current page you are on.

    Try my solution above, see how that works for you?

    Cheers, Lee.

  • MOthman 27 posts 89 karma points
    Apr 05, 2012 @ 14:38
    MOthman
    0

    Lee, I am trying to remove my solution but I couldn't, I realised it doesn't work properly

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Apr 05, 2012 @ 14:40
    Lee Kelleher
    0

    No worries MOthman, it's a restriction of the forum. :-)

    Cheers, Lee.

  • MOthman 27 posts 89 karma points
    Apr 05, 2012 @ 14:52
    MOthman
    0

    Lee, many thanks for your solution, with a few small tweeks I managed to get it working. I used your code but instead of decendant, we should use decendant-or-self, and sibling without s at the end (perhaps, it was my fault )

    The working code is 

     

    var sibling = Node.GetNodeByXpath(string.Concat("descendant-or-self::*[@isDoc and @id = ", _currentNode.Id, "]/preceding-sibling::* [@isDoc][1]"));
    if (sibling != null)
    {
    Response.Redirect(umbraco.library.NiceUrl(sibling.Id));
    }

    This is my first time to post on Umbraco forum but not the last :)

    This solution credit should go to Lee, cheers

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Apr 05, 2012 @ 15:11
    Lee Kelleher
    0

    Hi MOthman,

    Sorry about the typos... that's what I get for writing code off the top of my head! ;-)

    Glad that you got it sorted in the end.  Welcome to the Umbraco community!

    Cheers, Lee.

Please Sign in or register to post replies

Write your reply to:

Draft