Creating, Updating and Removing Calendar

Creating a Calendar

Calendars and other information is used by Microsoft Project to build project schedules. With Aspose.Tasks for .NET, adding a calendar to a project is easy. The Calendar class constructors allow you to assign an ID or a calendar name to the calendar, or use the default constructor. The options are described below.

There are three ways of creating a calendar in Aspose.Tasks. The Calendar class exposes the following three constructors:

The following code creates three different calendars, one using each constructor. Open the output file in Microsoft Project and, on the Project menu, select Project Information to access the calendars.

Project information showing the calendars

checking available calendars in Microsoft Project

1// Create a project instance
2Project project = new Project();
3
4// New calendar can be added to a project's calendar collection using the collection's Add method.
5Calendar cal1 = project.Calendars.Add("New Info");
6Calendar cal2 = project.Calendars.Add("no name");
7Calendar cal3 = project.Calendars.Add("cal3");
8
9project.Save("CreatingCalendar_out.Xml", SaveFileFormat.XML);

Defining Weekdays for Calendar

Microsoft Project keeps track of which days are considered weekdays in a calendar so that it can calculate project end dates and so on. Aspose.Tasks allows developers to define weekdays for a calendar associated with a project.

The Days collection exposed by the Calendar class is used to define the weekdays for a calendar. The Days collection represents an array list of WeekDay objects.

The CreateDefaultWorkingDay method exposed by the WeekDay class can further be implemented to define the default day timings, while the FromTime and ToTime properties exposed by the WorkingTime class is used to define the specific timing for a day. FromTime and ToTime support the DateTime data type.

The code samples below set weekdays for a project. After running the code, open the output file in Microsoft Project and, on the Tools menu, select Change Work Timing to establish that the changes have been applied.

Defining weekdays in Microsoft Project

adding week days in Microsoft Project calendars

The code below defines Monday through Thursday as weekdays with default timings, whereas Friday as a weekday with special timings.

 1// This example shows how Aspose.Tasks API can be used to define a new Calendar, add week days to it and define working times for days
 2// Create a project instance
 3Project project = new Project();
 4
 5// Define Calendar
 6Calendar cal = project.Calendars.Add("Calendar1");
 7
 8// Add working days monday through thursday with default timings
 9cal.WeekDays.Add(WeekDay.CreateDefaultWorkingDay(DayType.Monday));
10cal.WeekDays.Add(WeekDay.CreateDefaultWorkingDay(DayType.Tuesday));
11cal.WeekDays.Add(WeekDay.CreateDefaultWorkingDay(DayType.Wednesday));
12cal.WeekDays.Add(WeekDay.CreateDefaultWorkingDay(DayType.Thursday));
13cal.WeekDays.Add(new WeekDay(DayType.Saturday));
14cal.WeekDays.Add(new WeekDay(DayType.Sunday));
15
16// Set friday as short working day
17WeekDay myWeekDay = new WeekDay(DayType.Friday);
18
19// Sets working time. Only time part of DateTime is important
20WorkingTime wt1 = new WorkingTime();
21wt1.FromTime = new DateTime(1, 1, 1, 9, 0, 0, 0);
22wt1.ToTime = new DateTime(1, 1, 1, 12, 0, 0, 0);
23WorkingTime wt2 = new WorkingTime();
24wt2.FromTime = new DateTime(1, 1, 1, 13, 0, 0, 0);
25wt2.ToTime = new DateTime(1, 1, 1, 16, 0, 0, 0);
26myWeekDay.WorkingTimes.Add(wt1);
27myWeekDay.WorkingTimes.Add(wt2);
28myWeekDay.DayWorking = true;
29cal.WeekDays.Add(myWeekDay);
30
31project.Save("Project_DefineCalendarWeekdays_out.xml", SaveFileFormat.XML);

Replacing a Calendar with a New Calendar

Calendars are used to see whether resources are available, and when tasks are scheduled. There are different types of calendars. This article looks at how to replace a base calendar, that is, the calendar used for projects and tasks, with another one.

Base calendars come in different forms:

The list of all calendars can be retrieved as CalendarCollection using the Project class. The CalendarCollection can then be used to Remove or Add a new calendar as shown in the following code sample.

The following lines of code replace an existing calendar with a new standard calendar.

 1// Create project
 2Project project = new Project("New Project.mpp");
 3
 4// Access project calendars
 5CalendarCollection calColl = project.Calendars;
 6foreach (Calendar myCalendar in calColl)
 7{
 8    if (myCalendar.Name == "TestCalendar")
 9    {
10        // Remove calendar
11        calColl.Remove(myCalendar);
12    }
13}
14
15// Add new calendar
16Calendar newCalendar = calColl.Add("TestCalendar");
17project.Save("ReplaceCalendar_out.mpp", SaveFileFormat.MPP);

Making a Standard Calendar

A standard calendar provides the most common work days, work hours and holidays. By default, a standard calendar is added whenever a project is created using Microsoft Project. Aspose.Tasks has features for defining a standard calendar for a project.

The Calendar class exposes the following two static (shared in Visual Basic) overloaded methods for creating a standard calendar:

In Microsoft Project, open the file output by the code below and, on the Tools menu, select Change Working Times Information, to see the standard calendar.

The Change Working Times Information dialogue shows the standard calendar used by the project

editing calendar working times in Microsoft Project

The following lines of code create a standard calendar using both methods.

1// Create a project instance
2Project project = new Project();
3
4// Define Calendar and make it standard
5Calendar cal1 = project.Calendars.Add("My Cal");
6Calendar.MakeStandardCalendar(cal1);
7
8project.Save("Project_MakeStandardCalendar_out.xml", SaveFileFormat.XML);

Writing Updated Calendar Data to MPP

With Aspose.Tasks for .NET, you can update calendar data in a Microsoft Project MPP file and save it back.

The following code shows how to update the calendar data of a project by adding a new calendar and saving it back to the original MPP file. The steps involved in this activity are:

  1. Read the source MPP file.
  2. Add the calendar data to the project.
  3. Save the updated project data back to the MPP file.
 1Project project = new Project("New Project.mpp");
 2Calendar cal = project.Calendars.GetByUid(3);
 3
 4// Update the calendar information
 5Calendar.MakeStandardCalendar(cal);
 6cal.Name = "Test calendar";
 7CalendarException exc = new CalendarException();
 8exc.FromDate = DateTime.Now;
 9exc.ToDate = DateTime.Now.AddDays(2);
10exc.DayWorking = true;
11
12WorkingTime wt1 = new WorkingTime();
13wt1.FromTime = new DateTime(10, 1, 1, 9, 0, 0);
14wt1.ToTime = new DateTime(10, 1, 1, 13, 0, 0);
15
16WorkingTime wt2 = new WorkingTime();
17wt2.FromTime = new DateTime(10, 1, 1, 14, 0, 0);
18wt2.ToTime = new DateTime(10, 1, 1, 19, 0, 0);
19
20WorkingTime wt3 = new WorkingTime();
21wt3.FromTime = new DateTime(10, 1, 1, 20, 0, 0);
22wt3.ToTime = new DateTime(10, 1, 1, 21, 0, 0);
23
24exc.WorkingTimes.Add(wt1);
25exc.WorkingTimes.Add(wt2);
26exc.WorkingTimes.Add(wt3);
27cal.Exceptions.Add(exc);
28
29CalendarException exc2 = new CalendarException();
30exc2.FromDate = DateTime.Now.AddDays(7);
31exc2.ToDate = exc2.FromDate;
32exc2.DayWorking = false;
33cal.Exceptions.Add(exc2);
34
35project.Set(Prj.Calendar, cal);
36
37project.Save("WriteUpdatedCalendarDataToMPP_out.mpp", SaveFileFormat.MPP);
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.