Copied to clipboard

Flag this post as spam?

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


  • Richard 38 posts 78 karma points
    Feb 20, 2010 @ 12:04
    Richard
    0

    Getting the name of the current (page's) root node in C#?

    Hi, search as I may, it's a needle in a hay stack for API newbies. I have a Macro (xslt) that uses an <XsltExtensions> written in C# quite successfully. I can get at the dictionary but I cant figure out how to get that the current root node (name) in my content hierarchy. I can get it in XSLT (horror :-) but wheres the API in C# ? While I'm at it :-) if you happen to know how to then best redirect to another top level page via the API? Or do I just use standard ASP.NET Request.Redirect() or Server.Transfer()?

    Thanks!

    R

    R

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Feb 20, 2010 @ 13:07
    Morten Bock
    102

    Hi Richard

    The place to go in the API is umbraco.presentation.nodeFactory. 

    You would get the current node like this:

    Node current = Node.GetCurrent();

    You can then do a recursive method that looks at the current.Parent property. When the Parent.Id is -1 you are at the root node.

    If you are using domains, you can use this method:

    Domain[] domains = umbraco.library.GetCurrentDomains(Node.GetCurrent().Id);

    As you can see, this gets you an array of domains (you may have more than one). The Domain object has a RootNodeId that is the Id of the node where the domain is attached.

    For redirecting, you just use the Request.Redirect() as you suggest.

  • Jamie Howarth 306 posts 773 karma points c-trib
    Feb 20, 2010 @ 19:21
    Jamie Howarth
    0

    Richard,

    If you specify a property on your page called umbracoUrlRedirect and set it as a Content Picker type, Umbraco will automatically redirect to the node specified for you (done by ID, so even if the target page name changes, you don't lose any SEO).

    Hope this helps.

    Benjamin

Please Sign in or register to post replies

Write your reply to:

Draft