Adding Links for Tasks
Tasks linking is often required while working with Microsoft Project (MPP/XML) files. In VSTO, TaskDependencies.Add() is used for adding task links; Aspose.Tasks uses Project.TaskLinks.Add() to link tasks.
VSTO provides access to these links using MSProject.Project.Tasks.get_UniqueID(taskID).TaskDependencies. It provides a collection of dependencies that can be used to display and otherwise process requirements like access to detailed properties.
In Aspose.Tasks for .NET Project.TaskLinks provides a collection of links which can be used for display and accessing detailed properties of these links like LinkType, PredTask, SuccTask etc.
This article shows how to load MPP files in your .NET application and add/display link tasks using VSTO and Aspose.Tasks for .NET.
Link Tasks Using VSTO
To link a task using VSTO:
- Create a new project in Visual Studio.
- In the Solution Explorer, right-click and select Add Reference.
- Select the COM components tab, and select Microsoft Project 12.0 Object Library.
- Click OK.
This imports the Microsoft.Office.Interop.MSProject namespace at the start of your code. Use the code from the following example to link tasks.
1Microsoft.Office.Interop.MSProject.Application projectApplication = new Application();
2object missingValue = System.Reflection.Missing.Value;
3projectApplication.FileOpenEx(@"D:\Aspose\Migration\SampleProject.mpp",
4 missingValue, missingValue, missingValue, missingValue,
5 missingValue, missingValue, missingValue, missingValue,
6 missingValue, missingValue, PjPoolOpen.pjPoolReadOnly,
7 missingValue, missingValue, missingValue, missingValue,
8 missingValue);
9Microsoft.Office.Interop.MSProject.Project project = projectApplication.ActiveProject;
10project.Tasks.get_UniqueID(2).TaskDependencies.Add(project.Tasks.get_UniqueID(1), PjTaskLinkType.pjFinishToStart);
11project.Tasks.get_UniqueID(3).TaskDependencies.Add(project.Tasks.get_UniqueID(2), PjTaskLinkType.pjFinishToStart);
12project.Tasks.get_UniqueID(4).TaskDependencies.Add(project.Tasks.get_UniqueID(3), PjTaskLinkType.pjFinishToStart);
13project.Tasks.get_UniqueID(5).TaskDependencies.Add(project.Tasks.get_UniqueID(4), PjTaskLinkType.pjFinishToStart);
14project.Tasks.get_UniqueID(5).TaskDependencies.Add(project.Tasks.get_UniqueID(2), PjTaskLinkType.pjFinishToStart);
15// Display the dependencies
16foreach (Task tsk in project.Tasks)
17{
18 foreach (TaskDependency dep in project.Tasks.get_UniqueID(tsk.ID).TaskDependencies)
19 {
20 Console.WriteLine("From ID = " + dep.From.ID + "=>To ID = " + dep.To.ID);
21 }
22 Console.WriteLine("____________________________________________________________");
23}
24// Save the project
25projectApplication.FileCloseAll(Microsoft.Office.Interop.MSProject.PjSaveType.pjSave);
Link Tasks Using Aspose.Tasks for .NET
To link tasks in a project using Aspose.Tasks for .NET:
- Create a new project in Visual Studio.
- In the Solution Explorer, right-click and select Add Reference.
- Select .NET tab and select Aspose.Tasks.
- Click OK.
This imports the Aspose.Tasks namespace at the start of your code. Use the code from the following example to link tasks.
1Project project = new Project("New Project.mpp");
2
3Task task1 = project.RootTask.Children.GetById(1);
4Task task2 = project.RootTask.Children.GetById(2);
5Task task3 = project.RootTask.Children.GetById(3);
6Task task4 = project.RootTask.Children.GetById(4);
7Task task5 = project.RootTask.Children.GetById(5);
8
9// Link the tasks
10TaskLink taskLink = project.TaskLinks.Add(task1, task2, TaskLinkType.FinishToStart);
11taskLink = project.TaskLinks.Add(task2, task3, TaskLinkType.FinishToStart);
12taskLink = project.TaskLinks.Add(task3, task4, TaskLinkType.FinishToStart);
13taskLink = project.TaskLinks.Add(task4, task5, TaskLinkType.FinishToStart);
14taskLink = project.TaskLinks.Add(task2, task5, TaskLinkType.FinishToStart);
15
16// Display links among the tasks
17TaskLinkCollection allinks = project.TaskLinks;
18foreach (TaskLink link in allinks)
19{
20 Console.WriteLine("From ID = " + link.PredTask.Get(Tsk.Id) + " => To ID = " + link.SuccTask.Get(Tsk.Id));
21 Console.WriteLine("________________________________________");
22}
23
24project.Save("LinkTasks_out.mpp", SaveFileFormat.MPP);