0
votes

Umbraco newbie of course..

I have some code that returns a certain data type contents for a given node id which is detailed here:

umbraco API: trying to get the document type data for a given site node

That works fine, however I have to know the starting node i.e. in my case the site node id (multi-site umbraco installation).

I also know you can get the master root node via:

Umbraco - Finding Root Node in C#

My question is this...

From the backend API, is there anyway to dynamically get the current site node ID (first link)? this is so I don't have to know it in advance. I think there is an option to get the site id from a querystring.. Page.Request.QueryString["id"] however I am guessing this is returned if the page is a template page, however this is from a backend class?

Is there anyway? or I am barking up the wrong tree! ??

Any help would be appreciated!!

2

2 Answers

3
votes

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 ContentPlaceHolderID="ContentPlaceHolderDefault" runat="server">
    <!-- redirect to home page -->
</asp:Content> 
1
votes

Assuming that all of your rootnodes are of the same document type, in razor you could do something like this:

var folderId = @Model.AncestorOrSelf("MyFolderType").Id;

which will search from the current node 'up' or 'backwards' until it gets to a nodetype of the type specified.

another way is available here:

http://our.umbraco.org/forum/developers/api-questions/20742-Getting-the-home-node-using-c