Developer Forum »
How to move nodes programmatically
31 posts

Hi

I'm trying to create a method that I can use to move a node, kind of like when using drag and drop in the Edit view to move a node above, below or "into" another node. I've been having some trouble figuring out how to do this, so I'm wondering if you have some example code or a reference to methods/properties to get me started?

Ernad

181 posts

Hi!

To move a node, you have to first fetch the parent of the location you want to put the node in. The parent node has a Children property. The Children property has 5 move methods: Move, MoveAbove, MoveBelow, MoveFirst, MoveLast. 

Let me know if you figure it out!

31 posts

Great, thanks!

That was pretty easy. When I think about it, you actually told me this befor, my bad :)

I just added the following check because I ran into an error when trying to move a node that had a different parent-node than the target node.

if (!thisNode.Parent.IsSet() || thisNode.Parent.GetId() != targetParent.NodeId)
{
    thisNode.Parent.Set(targetParent);
    thisNode.UpdateChanges();
}

Also, I had to add this after calling the move-method:

targetParent.UpdateChanges();
1