Tasks and Calendars

In Microsoft Project, it is possible to associate specific calendars with individual tasks. Aspose.Tasks for .NET provides the same capability through the Tsk.Calendar property, which links a task to a particular calendar.

Tasks with Calendars

The Tsk class exposes the Calendar field used to set or get the calendar associated with a task. This property accepts or returns an Aspose.Tasks.Calendar object.

To create a calendar for a task in Microsoft Project:

  1. Open the project file.
  2. On the Project menu, select Change Working TimesCreate New Calendar.
  3. To assign the calendar, double-click a task in the Task Entry form and select the calendar in the task properties.

Example: Setting a Task Calendar

The following code shows how to create a calendar, add a task, and assign the calendar to it:

1Project project = new Project();
2
3// Add task
4Task task = project.RootTask.Children.Add("Task1");
5
6// Create calendar and assign to task
7Calendar cal = project.Calendars.Add("TaskCal1");
8task.Set(Tsk.Calendar, cal);

Example: Reading a Task Calendar

You can also traverse tasks in a project and read their associated calendars:

 1Project project = new Project("New Project.mpp");
 2
 3// Declare ChildTasksCollector class object
 4ChildTasksCollector collector = new ChildTasksCollector();
 5
 6// Use TaskUtils to get all children tasks in RootTask
 7TaskUtils.Apply(project.RootTask, collector, 0);
 8
 9// Parse all the recursive children
10foreach (Task task in collector.Tasks)
11{
12    Calendar cal = task.Get(Tsk.Calendar);
13    Console.WriteLine("Task calendar name: {0}", cal == null ? "None" : cal.Name);
14}

Key Notes

FAQ

Q: Can a task have more than one calendar?

Q: What happens if a task calendar conflicts with the project calendar?

Q: How can I remove a calendar from a task?

Q: Are resource calendars considered when assigning task calendars?

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.