Calculate Project Programmatically using C#

We can calculate project schedules or costs using calculation modes defined in Project options for Microsoft Project. A project schedule shows the start and end date of all project tasks. Microsoft Project allows the project calculation manually or automatically. As a C# developer, we can easily set project calculation mode in .NET applications and calculate project properties. In this article, we will learn how to calculate a project programmatically using C#.

The following topics shall be covered in this article:

C# API to Calculate Project Programmatically

For setting project calculation modes, we will be using the Aspose.Tasks for .NET API. It provides three project calculation modes, to calculate the values of dependent properties. These calculation modes are:

  • None – it sets only necessary properties and does not recalculate project dates and costs.
  • Manual – it sets only dependent object properties without recalculation of project dates and costs.
  • Automatic – it recalculates project dates and cost properties automatically.

The API defines above-mentioned calculation modes in the CalculationMode enumeration. The CalculationMode property of the Project class allows setting or getting the value of the CalculationMode. The API also allows manipulating an existing project in order to add some modifications. Moreover, it facilitates performing basic as well as advanced project management operations seamlessly. Please either download the DLL of the API or install it using NuGet.

PM> Install-Package Aspose.Tasks

Manual Project Calculation using C#

We can set the calculation mode to manual and verify it by following the steps given below:

  1. Firstly, create an instance of the Project class.
  2. Next, set calculation mode to Manual.
  3. Then, specify the project start date.
  4. Next, add new tasks, e.g., Task 1, and Task 2.
  5. Then, read task properties set in manual mode.
  6. After that, initialize an object of the TaskLinks class and link tasks.
  7. Finally, verify the start and end date of Task 2.

The following code sample shows how to manually calculate a project using C#.

// This code example demonstrates how to set Manual Project Calculation Mode.
// Create an instance of the Project
Project project = new Project();
// Set calculation mode to Manual
project.CalculationMode = CalculationMode.Manual;
// Set project start date
project.Set(Prj.StartDate, new DateTime(2015, 4, 15));
// Add new tasks
Aspose.Tasks.Task task1 = project.RootTask.Children.Add("Task 1");
Aspose.Tasks.Task task2 = project.RootTask.Children.Add("Task 2");
// The necessary properties are set in manual mode
Console.WriteLine("Task1.Id Equals 1 : {0} ", task1.Get(Tsk.Id).Equals(1));
Console.WriteLine("Task1 OutlineLevel Equals 1 : {0} ", task1.Get(Tsk.OutlineLevel).Equals(1));
Console.WriteLine("Task1 Start Equals 15/04/2015 08:00 AM : {0} ", task1.Get(Tsk.Start).Equals(new DateTime(2015, 4, 15, 8, 0, 0)));
Console.WriteLine("Task1 Finish Equals 15/04/2015 05:00 PM : {0} ", task1.Get(Tsk.Finish).Equals(new DateTime(2015, 4, 15, 17, 0, 0)));
Console.WriteLine("Task1 Duration Equals 1 day : {0} ", task1.Get(Tsk.Duration).ToString().Equals("1 day"));
Console.WriteLine("Task2 Start Equals 15/04/2015 08:00 AM : {0} ", task2.Get(Tsk.Start).Equals(new DateTime(2015, 4, 15, 8, 0, 0)));
Console.WriteLine("Task2 Finish Equals 15/04/2015 05:00 PM : {0} ", task2.Get(Tsk.Finish).Equals(new DateTime(2015, 4, 15, 17, 0, 0)));
Console.WriteLine("Task2 Duration Equals 1 day : {0} ", task2.Get(Tsk.Duration).ToString().Equals("1 day"));
// Link two tasks together their dates shall not be recalculated in manual mode
TaskLink link = project.TaskLinks.Add(task1, task2, TaskLinkType.FinishToStart);
// No change in Task 2 dates
Console.WriteLine("Task1 Start Equals Task2 Start : {0} ", task1.Get(Tsk.Start).Equals(task2.Get(Tsk.Start)));
Console.WriteLine("Task1 Finish Equals Task2 Finish : {0} ", task1.Get(Tsk.Finish).Equals(task2.Get(Tsk.Finish)));
Task1.Id Equals 1 : True
Task1 OutlineLevel Equals 1 : True
Task1 Start Equals 15/04/2015 08:00 AM : True
Task1 Finish Equals 15/04/2015 05:00 PM : True
Task1 Duration Equals 1 day : True
Task2 Start Equals 15/04/2015 08:00 AM : True
Task2 Finish Equals 15/04/2015 05:00 PM : True
Task2 Duration Equals 1 day : True
Task1 Start Equals Task2 Start : True
Task1 Finish Equals Task2 Finish : True

Automatic Project Calculation using C#

We can set the calculation mode to automatic and verify it by following the steps given below:

  1. Firstly, create an instance of the Project class.
  2. Next, set calculation mode to Automatic.
  3. Then, set the project start date.
  4. Now, add new tasks, e.g., Task 1, and Task 2.
  5. After that, initialize an object of the TaskLinks class and link tasks.
  6. Finally, verify the recalculated dates.

The following code sample shows how to calculate the project automatically using C#.

// This code example demonstrates how to set Automatic Project Calculation Mode.
// Create an instance of the Project
Project project = new Project();
// Set calculation mode to Automatic
project.CalculationMode = CalculationMode.Automatic;
// Set project start date
project.Set(Prj.StartDate, new DateTime(2015, 4, 15));
// Add new tasks
Aspose.Tasks.Task task1 = project.RootTask.Children.Add("Task 1");
Aspose.Tasks.Task task2 = project.RootTask.Children.Add("Task 2");
// Link tasks
TaskLink link = project.TaskLinks.Add(task1, task2, TaskLinkType.FinishToStart);
// Verify the recalculated dates
Console.WriteLine("Task1 Start + 1 Equals Task2 Start : {0} ", task1.Get(Tsk.Start).AddDays(1).Equals(task2.Get(Tsk.Start)));
Console.WriteLine("Task1 Finish + 1 Equals Task2 Finish : {0} ", task1.Get(Tsk.Finish).AddDays(1).Equals(task2.Get(Tsk.Finish)));
Console.WriteLine("RootTask Finish Equals Task2 Finish : {0} ", task2.Get(Tsk.Finish).Equals(project.RootTask.Get(Tsk.Finish)));
Console.WriteLine("Project Finish Date Equals Task2 Finish : {0} ", task2.Get(Tsk.Finish).Equals(project.Get(Prj.FinishDate)));
Task1 Start + 1 Equals Task2 Start : True
Task1 Finish + 1 Equals Task2 Finish : True
RootTask Finish Equals Task2 Finish : True
Project Finish Date Equals Task2 Finish : True

No Project Calculation Mode in C#

We can set the calculation mode to none and verify it by following the steps given below:

  1. Firstly, create an instance of the Project class.
  2. Next, set calculation mode to None.
  3. Then, specify the project start date.
  4. Next, add a new task, e.g., Task 1.
  5. Then, read task properties.
  6. After that, set task duration in days.
  7. Finally, verify the start and end date of the Task.

The following code sample shows how to set project calculation to none using C#.

// This code example demonstrates how to set Project Calculation Mode to None.
// Create an instance of the Project
Project project = new Project();
// Set calculation mode to None
project.CalculationMode = CalculationMode.None;
// Add a new task
Aspose.Tasks.Task task = project.RootTask.Children.Add("Task");
// Note that even ids were not calculated
Console.WriteLine("Task.Id Equals 0 : {0} ", task.Get(Tsk.Id).Equals(0));
Console.WriteLine("Task.OutlineLevel Equals 0 : {0} ", task.Get(Tsk.OutlineLevel).Equals(0));
Console.WriteLine("Task Start Equals DateTime.MinValue : {0} ", task.Get(Tsk.Start).Equals(DateTime.MinValue));
Console.WriteLine("Task Finish Equals DateTime.MinValue : {0} ", task.Get(Tsk.Finish).Equals(DateTime.MinValue));
Console.WriteLine("Task Duration Equals 0 mins : {0} ", task.Get(Tsk.Duration).ToString().Equals("0 mins"));
// Set duration property
task.Set(Tsk.Duration, project.GetDuration(2, TimeUnitType.Day));
Console.WriteLine("Task Duration Equals 2 days : {0} ", task.Get(Tsk.Duration).ToString().Equals("2 days"));
Console.WriteLine("Task Start Equals DateTime.MinValue : {0} ", task.Get(Tsk.Start).Equals(DateTime.MinValue));
Console.WriteLine("Task Finish Equals DateTime.MinValue : {0} ", task.Get(Tsk.Finish).Equals(DateTime.MinValue));
Task.Id Equals 0 : True
Task.OutlineLevel Equals 0 : False
Task Start Equals DateTime.MinValue : True
Task Finish Equals DateTime.MinValue : True
Task Duration Equals 0 mins : True
Task Duration Equals 2 days : True
Task Start Equals DateTime.MinValue  : True
Task Finish Equals DateTime.MinValue  : True

Get a Free License

You can get a free temporary license to try the library without evaluation limitations.

Conclusion

In this article, we have learned how to set project calculation modes programmatically. We have also seen how values of dependent properties are calculated in each calculation mode using C#. Besides, you can learn more about Aspose.Tasks for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also