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:

Below are examples of both approaches for linking tasks.

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:

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:

The API is intuitive, concise, and ready for server-side use in ASP.NET, Azure Functions, or background services.

Comparison Table

FeatureVSTO / InteropAspose.Tasks for .NET
Requires MS Project✅ Yes❌ No
Supported Environments🖥 Desktop only☁ Desktop, Web, Cloud, Containers
Dependency TypeCOM InteropPure .NET assembly
API Simplicity⚠ Verbose and boilerplate-heavy✅ Clean and object-oriented
Task Link ManagementTaskDependencies collectionTaskLinks 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:

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.