Working with Calendar Items in PST File

Adding MapiCalendar to PST

Create New PST, Add Sub-folders and Messages showed how to create a PST file and add a subfolder to it. With Aspose.Email you can add MapiCalendar to the Calendar subfolder of a PST file that you have created or loaded.

Below are the steps to add MapiCalendar to a PST:

  1. Create a MapiCalendar object.
  2. Set the MapiCalendar properties using a constructor and methods.
  3. Create a PST using the PersonalStorage.create() method.
  4. Create a pre-defined folder (Calendar) at the root of the PST file by accessing the root folder and then calling the addMapiMessageItem() method.

The code snippet below shows how to create a MapiCalendar and then add it to the Calendar folder of a newly created PST file.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2016);
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DAY_OF_MONTH, 1);
Date startDate = cal.getTime();
cal = Calendar.getInstance();
cal.set(Calendar.YEAR, 2016);
cal.set(Calendar.MONTH, Calendar.FEBRUARY);
cal.set(Calendar.DAY_OF_MONTH, 1);
Date endDate = cal.getTime();
// Create the appointment
MapiCalendar appointment = new MapiCalendar("LAKE ARGYLE WA 6743", "Appointment", "This is a very important meeting.", startDate, endDate);
// Create the meeting
MapiRecipientCollection attendees = new MapiRecipientCollection();
attendees.add("ReneeAJones@armyspy.com", "Renee A. Jones", MapiRecipientType.MAPI_TO);
attendees.add("SzllsyLiza@dayrep.com", "Szollosy Liza", MapiRecipientType.MAPI_TO);
MapiCalendar meeting = new MapiCalendar("Meeting Room 3 at Office Headquarters", "Meeting", "Please confirm your availability.", startDate,
endDate, "CharlieKhan@dayrep.com", attendees);
PersonalStorage pst = PersonalStorage.create(dataDir + "MapiCalendarToPST_out.pst", FileFormatVersion.Unicode);
FolderInfo calendarFolder = pst.createPredefinedFolder("Calendar", StandardIpmFolder.Appointments);
calendarFolder.addMapiMessageItem(appointment);
calendarFolder.addMapiMessageItem(meeting);

Save Calendar Items from Outlook PST to Disk in ICS format

This article shows how to access calendar items from an Outlook PST file and save the calendar to disk in ICS format. It uses the PersonalStorage and MapiCalendar classes to get the calendar information.

Below are the steps to save the calendar items:

  1. Load the PST file in the PersonalStorage class.
  2. Browse the Calendar folder.
  3. Get the contents of the Calendar folder to get the message collection.
  4. Loop through the message collection.
  5. Call the PersonalStorage.extractMessage() method to get the contact information in the MapiCalendar class.
  6. Call the MapiCalendar.save() method to save the calendar item to disk in ICS format.

The program below loads a PST file from disk and saves all the calendar items in ICS format. The ICS files can then be used in any other program that can load the standard ICS calendar file. If you open any ICS file in Microsoft Outlook, it will look like the one in the below screenshot.

todo:image_alt_text
Figure: Calendar item saved with Aspose.Email
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
// Load the Outlook PST file
PersonalStorage pst = PersonalStorage.fromFile(dataDir + "Outlook.pst");
// Get the Calendar folder
FolderInfo folderInfo = pst.getRootFolder().getSubFolder("Calendar");
// Loop through all the calendar items in this folder
MessageInfoCollection messageInfoCollection = folderInfo.getContents();
for (MessageInfo messageInfo : messageInfoCollection) {
// Get the calendar information
MapiCalendar calendar = (MapiCalendar) pst.extractMessage(messageInfo).toMapiMessageItem();
// Display some contents on screen
System.out.println("Name: " + calendar.getSubject());
// Save to disk in ICS format
calendar.save("Calendar: " + calendar.getSubject() + ".ics", AppointmentSaveFormat.Ics);
}

Extract Calendar Items from a PST File

MapiCalendar class represents a calendar item in the Microsoft Outlook MAPI format. Extract a message from a PST file and convert it into a MAPI message item. The following code sample extracts a calendar item from a PST file and converts it into a MapiCalendar object for further manipulation or processing:

MapiCalendar cal = (MapiCalendar) pst.extractMessage(messageInfo).toMapiMessageItem();

Save Calendar Items in ICS format with Original Timestamp

Use the above code sample to extract a calendar item from a PST file and then specify additional options to save it as ICS with original timestamp using the setKeepOriginalDateTimeStamp method of the MapiCalendarIcsSaveOptions class:

MapiCalendar cal = (MapiCalendar) pst.extractMessage(messageInfo).toMapiMessageItem();

if (cal != null) {
    MapiCalendarIcsSaveOptions so = new MapiCalendarIcsSaveOptions();
    so.setKeepOriginalDateTimeStamp(true);
    cal.save("cal.ics", so);
}

Modify/Delete Occurrences from Recurrences

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-Java
Date startDate = addHours(new Date(), 12);
MapiCalendarEventRecurrence recurrence = new MapiCalendarEventRecurrence();
recurrence.setRecurrencePattern(new MapiCalendarDailyRecurrencePattern());
MapiCalendarRecurrencePattern pattern = recurrence.getRecurrencePattern();
pattern.setPatternType(MapiCalendarRecurrencePatternType.Day);
pattern.setPeriod(1);
pattern.setEndType(MapiCalendarRecurrenceEndType.NeverEnd);
Date exceptionDate = addDays(startDate, 1);
// adding one exception
MapiCalendarExceptionInfo exceptionInfo = new MapiCalendarExceptionInfo();
exceptionInfo.setLocation("London");
exceptionInfo.setSubject("Subj");
exceptionInfo.setOriginalStartDate(exceptionDate);
exceptionInfo.setStartDateTime(exceptionDate);
exceptionInfo.setEndDateTime(addHours(exceptionDate, 5));
pattern.getExceptions().addItem(exceptionInfo);
pattern.getModifiedInstanceDates().addItem(exceptionDate);
// every modified instance also has to have an entry in the DeletedInstanceDates field with the original instance date.
pattern.getDeletedInstanceDates().addItem(exceptionDate);
// adding one deleted instance
pattern.getDeletedInstanceDates().addItem(addHours(exceptionDate, 2));
MapiRecipientCollection recColl = new MapiRecipientCollection();
recColl.add("recepient@gmail.com", "R1", MapiRecipientType.MAPI_TO);
MapiCalendar newCal = new MapiCalendar(
"This is Location",
"This is Summary",
"This is recurrence test",
startDate,
addHours(startDate, 3),
"organizer@domain.com",
recColl);
newCal.setRecurrence(recurrence);
ByteArrayOutputStream memory = new ByteArrayOutputStream();
try {
PersonalStorage pst = PersonalStorage.create(memory, FileFormatVersion.Unicode);
FolderInfo calendarFolder = pst.createPredefinedFolder("Calendar", StandardIpmFolder.Appointments);
calendarFolder.addMapiMessageItem(newCal);
}
finally {
memory.close();
}