Adding Links for Tasks
Tasks linking is a fundamental operation when modeling project dependencies in Microsoft Project (MPP/XML) files. This article demonstrates how to programmatically add and visualize task dependencies using both VSTO and Aspose.Tasks for .NET.
Introduction
In project management, task links define how activities depend on one another. These dependencies — such as Finish-to-Start or Start-to-Start — ensure that tasks follow a logical execution sequence.
When working with Microsoft Project programmatically, you can establish these links using either:
- VSTO and COM interop: tightly coupled with the Microsoft Project desktop application.
- Aspose.Tasks for .NET: a platform-independent, server-safe API for manipulating project files.
Below are examples of both approaches for linking tasks.
Link Tasks Using VSTO
The following example demonstrates how to create a chain of Finish-to-Start dependencies using Microsoft.Office.Interop.MSProject.
⚠ Requires Microsoft Project to be installed and accessible on the machine.
1Microsoft.Office.Interop.MSProject.Application projectApplication = new Application();
2object missingValue = System.Reflection.Missing.Value;
3
4projectApplication.FileOpenEx(@"D:\Aspose\Migration\SampleProject.mpp",
5 missingValue, missingValue, missingValue, missingValue,
6 missingValue, missingValue, missingValue, missingValue,
7 missingValue, missingValue, PjPoolOpen.pjPoolReadOnly,
8 missingValue, missingValue, missingValue, missingValue,
9 missingValue);
10
11Microsoft.Office.Interop.MSProject.Project project = projectApplication.ActiveProject;
12
13// Add task dependencies using Finish-to-Start links
14project.Tasks.get_UniqueID(2).TaskDependencies.Add(project.Tasks.get_UniqueID(1), PjTaskLinkType.pjFinishToStart);
15project.Tasks.get_UniqueID(3).TaskDependencies.Add(project.Tasks.get_UniqueID(2), PjTaskLinkType.pjFinishToStart);
16project.Tasks.get_UniqueID(4).TaskDependencies.Add(project.Tasks.get_UniqueID(3), PjTaskLinkType.pjFinishToStart);
17project.Tasks.get_UniqueID(5).TaskDependencies.Add(project.Tasks.get_UniqueID(4), PjTaskLinkType.pjFinishToStart);
18project.Tasks.get_UniqueID(5).TaskDependencies.Add(project.Tasks.get_UniqueID(2), PjTaskLinkType.pjFinishToStart);
19
20// Print dependencies to console
21foreach (Task tsk in project.Tasks)
22{
23 foreach (TaskDependency dep in project.Tasks.get_UniqueID(tsk.ID).TaskDependencies)
24 {
25 Console.WriteLine("From ID = " + dep.From.ID + " => To ID = " + dep.To.ID);
26 }
27 Console.WriteLine("____________________________________________________________");
28}
29
30// Save and close the project
31projectApplication.FileCloseAll(Microsoft.Office.Interop.MSProject.PjSaveType.pjSave);
Key Notes:
TaskDependencies.Add()
links a predecessor and successor using a definedPjTaskLinkType
.- Interop requires verbose code and careful handling of COM objects.
- The
get_UniqueID(taskID)
method is used to access specific tasks for linking.
Link Tasks Using Aspose.Tasks for .NET
Aspose.Tasks offers a modern and streamlined way to define task dependencies programmatically without relying on Microsoft Project or COM components.
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);
Highlights:
project.TaskLinks.Add(task1, task2, TaskLinkType.FinishToStart)
adds a direct link between any two tasks.- All task relationships are stored in the
TaskLinkCollection
. - Each link exposes properties like
PredTask
,SuccTask
,LinkType
, andLag
.
The API is intuitive, concise, and ready for server-side use in ASP.NET, Azure Functions, or background services.
Comparison Table
Feature | VSTO / Interop | Aspose.Tasks for .NET |
---|---|---|
Requires MS Project | ✅ Yes | ❌ No |
Supported Environments | 🖥 Desktop only | ☁ Desktop, Web, Cloud, Containers |
Dependency Type | COM Interop | Pure .NET assembly |
API Simplicity | ⚠ Verbose and boilerplate-heavy | ✅ Clean and object-oriented |
Task Link Management | TaskDependencies collection | TaskLinks collection |
Cross-platform support | ❌ No | ✅ Yes |
Conclusion
Linking tasks is critical for maintaining project logic. While VSTO offers native access to Microsoft Project, its reliance on the desktop environment and COM interop makes it less suitable for modern applications.
Aspose.Tasks for .NET provides a clean and scalable alternative that simplifies task relationship management. Its flexibility and platform independence make it ideal for integrating into CI/CD pipelines, server apps, and web platforms.
To learn more about task relationships or scheduling logic, explore: