Search TreeNode and delete children of a specific child

Sylvain C. asked on July 17, 2019 04:21

Hi, In my MVC project, I have the following configuration in my tree:

Root
    Section 1
        Container Type A
            Page 1
            page 2

I have some code which search Section 1 in order to check if it exists:

TreeNode parentPage = tree.SelectNodes()                  
                        .Path(_path)                                        
                        .OnCurrentSite()
                        .FirstOrDefault();

After veryfying that this node exists, I would like to search it to see if it contains a container of Type A and if yes, delete this specific container and all its children including all cultures (so in my case, I would delete the Container Type A and its 2 children for example)

Thank you very much for your help

Sylvain

Correct Answer

Dmitry Bastron answered on July 18, 2019 10:17

I'm not sure, what do you mean by 'directly'?

First, you need to retrieve TreeNode, for example with DocumentHelper class (refer to this article for more details and examples).

When you have a TreeNode retrieved from DB it's got 2 properties:

// direct child nodes
var directChildren = treeNode.Children;

// all child nodes (from all sublevels)
var allChildren = treeNode.AllChildren;

But be careful with these methods. When you access this children collection a new request to the database will happen to retrieve this data.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Dmitry Bastron answered on July 17, 2019 10:22

Hi Sylvain,

The following code should help you (assuming TypeA is one of your Page Types):

var containerA = DocumentHelper.GetDocuments<TypeA>()
    .WhereStartsWith("NodeAliasPath", _path)
    .TopN(1)
    .FirstOrDefault();

containerA?.DeleteAllCultures();
0 votesVote for this answer Mark as a Correct answer

Sylvain C. answered on July 17, 2019 19:30

Thank you Dmitry for your quick reply. I will do as suggested. Is there any way of using the TreeNode directly and check its children directly?

Thank you again

Sylvain

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.