We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Is there a way to duplicate my task?

I have around 20 tasks in a project and it has predecessors too. I want to duplicate these 20 tasks 5 times in the same project. When I duplicate this, it needs to maintain the predecessors within the set of tasks. can you please help me?


I tried the below code, but no luck.
Project blankMpp = new Project();
this._Project.CopyTo(blankMpp);
this._Project.CopyTo(blankMpp);
this._Project.CopyTo(blankMpp);
this._Project.CopyTo(blankMpp);
this._Project.CopyTo(blankMpp);

Hi Sanjay,


Thank you for contacting Aspose support team.

I have analyzed your code and would like to share that CopyTo() function overwrites all the previous data in the destination blankMPP file that is why calling this function again and again does not append the tasks but overwrites them.

Task copying will not work as well because each task in the source MPP has its ID and UID which does not change during copy, thus this operation does not append tasks but overwrites them as shown in the following sample code.

Project prj = new Project(@“Tasks20.mpp”);
Project blankMpp = new Project();
for (int j = 0; j < 5; j++ )
{
for (int i = 0; i < prj.RootTask.Children.Count; i++)
{
/*
blankMpp.RootTask.Children.Add(prj.RootTask.Children[i]);
/

//OR

/
Aspose.Tasks.Task temp = blankMpp.AddTask();
temp = prj.RootTask.Children[i];
*/
}
}
I am afraid that currently no such feature is available which may copy task by maintaining its predecessors as well. This requirement may be achieved by implementing own logic programatically.

Hey, No Problem I made it work and works like charm. Do you know how?


Like this,
ProjectPlanDocument plan = new ProjectPlanDocument();
plan.TaskItems.Add(new TaskItem { Id = 1, Name = “First” });
plan.TaskItems.Add(new TaskItem { Id = 2, Name = “Second” });
plan.TaskItems.Add(new TaskItem { Id = 3, Name = “Third” });

plan.Dependencies.Add(new Dependency { From = “1”, To = “3” });
plan.Dependencies.Add(new Dependency { From = “2”, To = “3” });

Project p = new Project();

int dynamicNumber = 1000;

for (int i = 0; i < 3; i++)
{
foreach (var task in plan.TaskItems)
p.RootTask.Children.Add(new Task { Name = task.Name, Id = task.Id + dynamicNumber });
ChildTasksCollector collector = new ChildTasksCollector(); // Create a ChildTasksCollector instance
TaskUtils.Apply(p.RootTask, collector, 1); // Collect all the tasks from RootTask using TaskUtils


foreach (var depe in plan.Dependencies)
{
var fromTask = collector.Tasks.Where(t => t.Id == int.Parse(depe.From) + dynamicNumber).FirstOrDefault();
var toTask = collector.Tasks.Where(t => t.Id == int.Parse(depe.To) + dynamicNumber).FirstOrDefault();

p.TaskLinks.Add(new TaskLink(fromTask, toTask, TaskLinkType.FinishToStart));
}
p.CalcTaskIds();
p.UpdateReferences();
dynamicNumber += 1000;
}

foreach (var task in p.RootTask.Children)
System.Diagnostics.Trace.Write(task.Id);
foreach (var link in p.TaskLinks)
System.Diagnostics.Trace.Write(link.PredTask.Id);

Hi Sanjay,


Thank you for sharing your sample code and we are glad to know that your issue is resolved. Please feel free to write us back if you have any other query related to Aspose.Tasks.