Working with Calendar Properties
Overview
Calendars in Microsoft Project define the working and non-working time across the entire project, as well as for individual tasks and resources. Aspose.Tasks for .NET provides programmatic access to these calendars, including their base hierarchy, naming, and unique identification.
This article describes how to access and modify general properties of calendars using the Aspose.Tasks API.
Key Calendar Properties
The
Calendar
class exposes several core properties that define the calendar’s identity and structure:
BaseCalendar
: References another calendar from which the current calendar inherits working time. This property is only applicable if the calendar is not itself a base calendar.Name
: The human-readable name of the calendar, used to distinguish it in the user interface and reports.Uid
: A unique integer identifier used to track and reference calendars within the project file.
These properties are critical when working with calendar collections, assigning calendars to resources, or exporting project structure to other formats.
Reading Calendar Metadata
The following code example demonstrates how to read and display general calendar properties such as name, UID, and base calendar reference.
1// Load an existing project
2Project project = new Project("Project_GeneralCalendarProperties.xml");
3
4foreach (Calendar cal in project.Calendars)
5{
6 if (cal.Name != null)
7 {
8 Console.WriteLine("UID : " + cal.Uid.ToString() + " Name: " + cal.Name);
9
10 // Show if it is has a base calendar
11 Console.Write("Base Calendar : ");
12 if (cal.IsBaseCalendar)
13 Console.WriteLine("Self");
14 else
15 Console.WriteLine(cal.BaseCalendar.Name);
16
17 // Get Time in hours on each working day
18 foreach (WeekDay wd in cal.WeekDays)
19 {
20 TimeSpan ts = wd.GetWorkingTime();
21 Console.WriteLine("Day Type: " + wd.DayType.ToString() + " Hours: " + ts.ToString());
22 }
23 }
24}
Use Case Example
These properties are commonly used in the following scenarios:
- Generating documentation or reports with calendar summaries.
- Performing validation or transformation of project structure.
- Managing inheritance chains between base calendars and dependent task/resource calendars.
By understanding and manipulating these core properties, developers can better manage scheduling logic and ensure compatibility with Microsoft Project expectations.