Moving Tasks
Contents
[
Hide
Show
]Microsoft Project allows moving one task under another task. In this case, the child tasks of the selected tasks are also moved with it. Aspose.Tasks for C++ API provides the same feature by moving a task under another task. This can be achieved by adding the selected task to the children of the new parent as shown in this article. The task can be moved under a different parent or the same parent.
Move Task Under Another Parent
Please note that using CalculationMode.None can improve performance when you add several tasks and call Recalculate method one time.
1// The path to the documents directory.
2System::String dataDir = RunExamples::GetDataDir(System::Reflection::MethodBase::GetCurrentMethod(ASPOSE_CURRENT_FUNCTION)->get_DeclaringType().get_FullName());
3
4// Loading project file
5System::SharedPtr<Project> project = System::MakeObject<Project>(dataDir + u"MoveTask.mpp");
6
7// Set CalculationMode
8project->set_CalculationMode(Aspose::Tasks::CalculationMode::Automatic);
9
10// Get Tasks by Ids
11System::SharedPtr<Task> t6 = project->get_RootTask()->get_Children()->GetByUid(6);
12System::SharedPtr<Task> t3 = project->get_RootTask()->get_Children()->GetByUid(3);
13
14// Adding Task 6 to another parent
15t3->get_Children()->Add(t6);
16
17// Saving File To Disk
18project->Save(dataDir + u"MoveTaskUnderAnotherParent_out.mpp", Aspose::Tasks::Saving::SaveFileFormat::MPP);
Moving Task Under the Same Parent
The MoveToSibling method allows moving a task under the same parent to a specific position.
1// The path to the documents directory.
2System::String dataDir = RunExamples::GetDataDir(System::Reflection::MethodBase::GetCurrentMethod(ASPOSE_CURRENT_FUNCTION)->get_DeclaringType().get_FullName());
3
4// Loading project file
5System::SharedPtr<Project> project = System::MakeObject<Project>(dataDir + u"MoveTask.mpp");
6
7// Move tasks with id 5 before task with id 3
8System::SharedPtr<Task> task = project->get_RootTask()->get_Children()->GetById(5);
9task->MoveToSibling(3);
10
11// Saving File To Disk
12project->Save(dataDir + u"MoveTaskUnderSameParent_out.mpp", Aspose::Tasks::Saving::SaveFileFormat::MPP);
Add Task To The End
To add a task to the end of the collection use -1.
1// The path to the documents directory.
2System::String dataDir = RunExamples::GetDataDir(System::Reflection::MethodBase::GetCurrentMethod(ASPOSE_CURRENT_FUNCTION)->get_DeclaringType().get_FullName());
3
4// Loading project file
5System::SharedPtr<Project> project = System::MakeObject<Project>(dataDir + u"MoveTask.mpp");
6
7// Move tasks with id 2 to the end of the collection
8System::SharedPtr<Task> task = project->get_RootTask()->get_Children()->GetById(2);
9task->MoveToSibling(-1);
10
11// Saving File To Disk
12project->Save(dataDir + u"MoveTaskAtTheEnd_out.mpp", Aspose::Tasks::Saving::SaveFileFormat::MPP);
Setting a Task to a SubProject
1// Create project instance
2System::String dataDir = RunExamples::GetDataDir(System::Reflection::MethodBase::GetCurrentMethod(ASPOSE_CURRENT_FUNCTION)->get_DeclaringType().get_FullName());
3System::SharedPtr<Project> project = System::MakeObject<Project>(dataDir + u"SubProjectTask.mpp");
4
5// Add task
6System::SharedPtr<Task> task = project->get_RootTask()->get_Children()->Add(u"Task 1");
7
8// Setting new subproject link
9task->Set<System::String>(Tsk::SubprojectName(), dataDir + u"subProject.mpp");
10
11// Save project
12project->Save(dataDir + u"SubProjectTask_out.mpp", Aspose::Tasks::Saving::SaveFileFormat::MPP);