Moving Tasks
In Microsoft Project, tasks can be moved within the task hierarchy to reflect changes in project structure.
- Moving a task under a new parent also moves all its child tasks.
- Aspose.Tasks for .NET provides APIs to replicate this behavior programmatically.
Moving a Task Under Another Parent
Aspose.Tasks allows reassigning a task to a new parent. This is useful when reorganizing project hierarchies.
For better performance when moving multiple tasks, set the project’s calculation mode to
CalculationMode.Noneand call Recalculate only once after all moves are completed.
Example:
1Project project = new Project("New Project.mpp");
2project.CalculationMode = CalculationMode.Automatic;
3// Get Tasks by Ids
4Task task1 = project.RootTask.Children.GetByUid(6);
5Task task2 = project.RootTask.Children.GetByUid(3);
6// Adding Task 6 to another parent
7task2.Children.Add(task1);
8project.Save("MoveTaskUnderAnotherParent_out.mpp", SaveFileFormat.MPP);This example demonstrates how to move a task under a different parent task.
Moving a Task Under the Same Parent
If you need to reorder tasks under the same parent, use the MoveToSibling method.
Example:
1Project project = new Project("New Project.mpp");
2// Move tasks with id 5 before task with id 3
3Task task = project.RootTask.Children.GetById(5);
4task.MoveToSibling(3);
5project.Save("MoveTaskUnderSameParent_out.mpp", SaveFileFormat.MPP);This example shows how to move a task to a new position within the same parent.
Adding a Task to the End
When adding or moving tasks, you can append them to the end of a collection by using -1 as the target index.
Example:
1Project project = new Project("New Project.mpp");
2// Move tasks with id 2 to the end of the collection
3Task task = project.RootTask.Children.GetById(2);
4task.MoveToSibling(-1);
5project.Save("MoveTaskAtTheEnd_out.mpp", SaveFileFormat.MPP);This example moves a task to the end of its parent’s task list.
Key Notes
- Hierarchy reorganization is common during project updates and restructuring.
- Performance optimization is recommended when moving multiple tasks — disable automatic recalculation until all changes are applied.
- Child tasks are always moved together with their parent task when reassigned.
- Works seamlessly with both MPP and XML formats.
FAQ
Q: When I move a task under another parent, will its child tasks move too?
- Yes. All child tasks are moved automatically with the parent task.
Q: How can I move multiple tasks efficiently?
- Use
CalculationMode.Nonebefore moving tasks, then callRecalculate()once after all operations.
Q: Can I reorder tasks without changing their parent?
- Yes. Use the
MoveToSiblingmethod to change a task’s position under the same parent.
Q: Does this functionality work with both MPP and XML project formats?
- Yes. Task hierarchy operations are supported consistently across all formats handled by Aspose.Tasks.