ClosDesign
ClosDesign

Reputation: 3924

Umbraco 4.11 get Parent Node

Should be a simple answer.

I am using Umbraco 4.11 and I need to get the parent Node of a cast node. I am a bit of a noob to C# and I am fixing a control someone else made. Should be simple but it was originally written before the DLL update from 4.7 to 4.11.

So below is my code. I need to get the parent Node. What would be the correct syntax to do this. You can see where the old code is commented out.

Thanks in advance.

 //New using
 using umbraco.NodeFactory;

 private string GetEmailContactProperty()
    {
        Node node = Node.GetCurrent();
        string email = null;

        do
        {
            if (node.NodeTypeAlias == NodeTypeAlias)
            {
                email = node.GetProperty("emailContact").Value;

                if (!String.IsNullOrEmpty(email))
                    break;
            }

            //node = node.Parent;
           //***Need Parent Node here. new Node is asking for Overload.
            node = new Node().Parent;

        } while(node.Parent.Id > -1);

Upvotes: 1

Views: 2346

Answers (2)

Sébastien Richer
Sébastien Richer

Reputation: 1701

Basically, from your code you want to traverse up the tree, across your ancestors, to find a property named "emailContact" that is not IsNullOrEmpty.

I think what you are looking for is a piece of code like this:

var emailContact = CurrentModel.AncestorsOrSelf().Items.Where(n => !string.IsNullOrWhiteSpace(n.GetProperty("emailContact").Value))

Another way would be to get property with the recursive flag set to true like so:

var emailContact = Model.GetProperty("emailContact ", true).Value; 

(See this post: http://our.umbraco.org/forum/developers/razor/19005-Recursive-fields-using-Razor-macro?p=1)

On a side not, it looks like you are currently working with Documents and not "content nodes", is this a back office control or a front end control?

Hope this helps

Upvotes: 0

ProNotion
ProNotion

Reputation: 3692

The original code should do what you are asking with regards to getting the parent node.

node = node.Parent;

Upvotes: 2

Related Questions