Handling Task Constraints

Task constraints are used to define a Microsoft Project project’s schedule and define when tasks should be started or finished. Constraints can be flexible - start or end as soon as possible or as late as possible - or inflexible. Inflexible constraints are tied to specific dates.

Working with Constraints

The ConstraintDate and ConstraintType properties are exposed by the static class Tsk class to handle constraints:

Setting Constraints in Microsoft Project

To set constraints in Microsoft Project:

  1. On the View menu, select More Views and then Task Entry Form.
  2. Double click a task on the Task Entry Form.
  3. Select the Advanced tab.
  4. Set a constraint by selecting an option from the Constraint type list and a date for the Constraint date list.

Setting Constraints with Aspose.Tasks

The constraint date is NA when the constraint type is As Soon As Possible or As Late As Possible. For date values equal to NA, Aspose.Tasks uses the value “1/1/2000” in the evaluation version, and DateTime.MinValue for the licensed product. In the below case, we take a source project file as an input and apply different types of constraints on various tasks in each case. The following code samples show the application of different Constraint types.

The code samples below set the constraint type set to Start No Earlier Than.

 1Project project = new Project("New Project.mpp");
 2
 3// Set constraint Start No Earlier Than on task with Id 1
 4Task summary = project.RootTask.Children.GetById(1);
 5summary.Set(Tsk.ConstraintType, ConstraintType.StartNoEarlierThan);
 6summary.Set(Tsk.ConstraintDate, new DateTime(2016, 12, 1, 9, 0, 0));
 7
 8SaveOptions o = new PdfSaveOptions();
 9o.StartDate = project.Get(Prj.StartDate);
10o.Timescale = Timescale.ThirdsOfMonths;
11project.Save("StartNoEarlierThan_out.pdf", o);

The code samples below set the constraint type set to Finish No Earlier Than.

1// Set constraint Finish No Earlier Than on task with Id 2
2Task first = project.RootTask.Children.GetById(2);
3first.Set(Tsk.ConstraintType, ConstraintType.FinishNoEarlierThan);
4first.Set(Tsk.ConstraintDate, new DateTime(2016, 12, 1, 18, 0, 0));
5SaveOptions options = new PdfSaveOptions();
6options.StartDate = project.Get(Prj.StartDate);
7options.Timescale = Timescale.ThirdsOfMonths;
8project.Save("FinishNoEarlierThan_out.pdf", options);

The code samples below set the constraint type set to Must Start On.

1// Set constraint Must Start On for task with Id 5
2Task roof = project.RootTask.Children.GetById(5);
3roof.Set(Tsk.ConstraintType, ConstraintType.MustStartOn);
4roof.Set(Tsk.ConstraintDate, new DateTime(2017, 1, 1, 9, 0, 0));
5
6SaveOptions options = new PdfSaveOptions();
7options.StartDate = project.Get(Prj.StartDate);
8options.Timescale = Timescale.ThirdsOfMonths;
9project.Save("MustStartOn_out.pdf", options);

The code samples below set the constraint type set to As Late As Possible.

1// Set constraint As Late As Possible for task with Id 11
2Task wallBoard = project.RootTask.Children.GetById(11);
3wallBoard.Set(Tsk.ConstraintType, ConstraintType.AsLateAsPossible);
4
5SaveOptions options = new PdfSaveOptions();
6options.StartDate = project.Get(Prj.StartDate);
7options.Timescale = Timescale.ThirdsOfMonths;
8project.Save("AsLateAsPossible_out.pdf", options);

The code sample below shows the constraint type set to Must Finish On.

1// Set constraint Must Finish On for task with Id 15
2Task task = project.RootTask.Children.GetById(15);
3task.Set(Tsk.ConstraintType, ConstraintType.MustFinishOn);
4task.Set(Tsk.ConstraintDate, new DateTime(2017, 3, 1, 18, 0, 0));
5
6SaveOptions options = new PdfSaveOptions();
7options.StartDate = project.Get(Prj.StartDate);
8options.Timescale = Timescale.ThirdsOfMonths;
9project.Save("MustFinishOn_out.pdf", options);

Getting Constraints

This code sample displays any constraints found when traversing the tasks in the project to a command window.

 1Project project = new Project("New Project.mpp");
 2
 3// Create a ChildTasksCollector instance
 4ChildTasksCollector collector = new ChildTasksCollector();
 5
 6// Collect all the tasks from RootTask using TaskUtils
 7TaskUtils.Apply(project.RootTask, collector, 0);
 8
 9// Parse through all the collected tasks
10foreach (Task task in collector.Tasks)
11{
12    if (task.Get(Tsk.ConstraintDate).ToShortDateString() == "1/1/2000")
13        Console.WriteLine("NA");
14    else
15        Console.WriteLine(task.Get(Tsk.ConstraintDate).ToShortDateString());
16
17    Console.WriteLine(task.Get(Tsk.ConstraintType).ToString());
18}
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.