Copied to clipboard

Flag this post as spam?

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


  • SinkyPars 132 posts 175 karma points
    May 26, 2011 @ 15:55
    SinkyPars
    0

    Getting the home node using c#

    Hi guys,

    I need to find the home node (type homepage) using c# on a multisite installation of umbraco.

    I cant find any examples of this anywhere. Any help would be much appreciated.

    Thanks

    Scott

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    May 26, 2011 @ 16:36
    Hendy Racher
    2

    Hi SinkyPars,

    How about:

    using umbraco.presentation.nodeFactory;

    Node node = Node.GetCurrent();
    while (node != null && node.NodeTypeAlias != "homepage")
    {
    node = node.Parent;
    }

    or if you have uComponents installed, how about this:

    using System.Linq;
    using umbraco.presentation.nodeFactory; using uComponents.Core; using uComponents.Core.uQueryExtensions;
     
    Node homepageNode = uQuery.GetCurrentNode()
    .GetAncestorOrSelfNodes()
    .Where(node => node.NodeTypeAlias == "homepage")
    .FirstOrDefault();

    or

    using System.Linq;
    using umbraco.presentation.nodeFactory;
    using uComponents.Core;

    Node homepageNode = uQuery.GetNodesByXPath("$currentPage/ancestor-or-self::homepage")
    .FirstOrDefault();

    HTH,

    Hendy

  • León van de Pavert 15 posts 37 karma points
    Jan 08, 2013 @ 17:04
    León van de Pavert
    0

    Just posted this as an answer to http://stackoverflow.com/questions/12643537/is-there-a-way-to-return-the-current-site-node-id-via-the-api-in-umbraco

    Say you have an Umbraco installation with two sites with their respective Homepages and Pages, e.g

    • Content (-1)
      • Homepage 1 (1000)
        • Page 1.1 (1001)
      • Homepage 2 (1002)
        • Page 2.1 (1003)

    In C# the current node can be obtained with

    Node currentNode = Node.GetCurrent();

    and its corresponding home node can be found with

    Node currentHome = new Node(int.Parse(currentNode.Path.Split(',')[1]));

    Now, currentNode.Path returns a string of comma separated integers that starts with -1, i.e. the root, the master root as you called it, under which all Homepages 'live'.

    E.g. the path value of Page 2.1 is "-1,1002,1003". When split at the comma, you'll end up with an array with 3 elements indexed 0,1,2. Now, the second one, with index 1 will give the id of the home node. As you can see, the last id is the id of the current node. As an aside, the indexes also tell the level of the node, so the level of a Homepage is 1.

    I used the following script on a template that was used on an intranet/extranet and has protected pages. When a visitor follows a link to a protected page, he/she is denied access and redirected to the homepage, which has a member login.

    <%@ Master Language="C#" MasterPageFile="~/umbraco/masterpages/default.master" AutoEventWireup="true" %>

    <%@ Import Namespace="umbraco.NodeFactory" %>

    <script runat="server" language="CSharp">
        protected void Page_Load(object sender, EventArgs e)
        {
            // prevents template to be run without proper authorisation
            Node currentNode = Node.GetCurrent();
            Node currentHome = new Node(int.Parse(currentNode.Path.Split(',')[1]));
            Boolean HasAccess = umbraco.library.HasAccess(currentNode.Id, currentNode.Path);
            Boolean IsProtected = umbraco.library.IsProtected(currentNode.Id, currentNode.Path);

            if (IsProtected && !HasAccess)
            {
                // redirect to ancestor-or-self::HomePage
                Response.Status = "403 Forbidden";
                Response.Redirect(umbraco.library.NiceUrl(currentHome.Id), true);
            }
        }
    </script>

    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolderDefault" runat="server">
        <!-- redirect to home page -->
    </asp:Content>

    Cheers,
    León

Please Sign in or register to post replies

Write your reply to:

Draft