0

I am using kentico application where i have to delete all child items which contains either node id of parent or parentnodeid of parent based on the child inside there. Below is the screen shot:

Tree Image 1

In this picture we can see we have a parent node called membership and inside one child called Blog. Under of Blog which node id is 583 having 29 child. Now I am running a code and getting all the child data under of that parent as a list. Below is the screen shot: List of parent and child image

Now i have to remove all the child data from list whose parentnodeid or parent-parent node id is 583 (Blog).

Below is the code used to get the list of all parent and child items:

 TreeProvider tree = new TreeProvider();
    var childNodeDataSet = tree.SelectNodes(childNodeId, null, SiteContext.CurrentSiteName);
    var getAllChildItems = childNodeDataSet.Items[0].AllChildren;
    var getItems = getAllChildItems.ToList().OrderBy(a=>a.NodeAliasPath);
As we can see above code childNodeId is nothing but the Membership page node id. Using this i am getting all the child items. Now in this getItems i have all the values where i have to remove Blog page parent and child completely.

I tried this code to remove the matching parent item from list:

 string excludeFileName = "583, 683, 686, 687";
                    string[] arrStringList = excludeFileName.Split(',');
                    foreach (var excName in arrStringList)
                    {
                        getItems.RemoveAll(a => (a.NodeID == Convert.ToInt32(excName.Trim()) || a.NodeParentID == Convert.ToInt32(excName.Trim()))
                        || (a.Parent.NodeID == Convert.ToInt32(excName.Trim()) || a.Parent.NodeParentID == Convert.ToInt32(excName.Trim()))
                        || (a.Parent.Parent.NodeID == Convert.ToInt32(excName.Trim()) || a.Parent.Parent.NodeParentID == Convert.ToInt32(excName.Trim()))
                        || (a.Parent.Parent.Parent.NodeID == Convert.ToInt32(excName.Trim()) || a.Parent.Parent.Parent.NodeParentID == Convert.ToInt32(excName.Trim())));
                    }

This code is working fine if i know particular node has how many level.

But if some of the node don't have that much level then in that case this code is giving exception.

If found any issues please let me know.

3
  • 1
    What you have provided "looks good". What you might want to post is your code so that can be reviewed to ensure you're using the API calls correctly. Jun 7, 2017 at 14:04
  • I have edited my question and added code to get understand how the list is generating. I also added code which i used for deleting node. Jun 8, 2017 at 7:18
  • So what's the exact question or problem then? Jun 10, 2017 at 16:39

1 Answer 1

0

I am a little confused at the path you're taking. If you want to get all children of a particular node, why not just get all nodes where the NodeAliasPath is like "The/Parent/Node/Path"+"/%"?

This will select all the children nodes all the way down.

If you only want to grab a certain number of levels, you can get the Parent's NodeLevel, and then in the query add a NodeLevel <= "+(ParentNodeLevel+2) to grab all the children and grandchildren only.

To exclude certain nodes, you can also put a "NodeID not in (123,234)".

Perhaps if you can clearly say what you are trying to accomplish we can help out easier!

PS: I Put the SQL, but the DocumentQuery object has most of these in methods, like .WhereNotIn("NodeID", ListOfNodeIDsToExclude)

1
  • Through NodeAliasPath i am able to remove items from the list. Thanks for your response. Jun 18, 2017 at 7:45

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.