Managing Calendar Items in PST Files

Add Calendar Events to PST Files

Create New PST File and Add Subfolders demonstrated how to create a PST file and include subfolders within it. With Aspose.Email, you can also add a MapiCalendar to the Calendar subfolder of an existing or newly created PST file. Below are the steps to add a MapiCalendar to a PST file:

  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 add_mapi_message_item() method.

The following code snippet shows you how to create a MapiCalendar and then add it to the calendar folder of a newly created PST file:

Save Outlook Calendar Items as ICS Files

This article explains how to access calendar items from an Outlook PST file and save them to disk in ICS format. You’ll need to use the PersonalStorage and MapiCalendar classes to retrieve the calendar data. Follow these steps to save calendar items:

  1. Load the PST file using the PersonalStorage class.
  2. Navigate to the Calendar folder.
  3. Retrieve the message collection from the Calendar folder.
  4. Loop through the message collection.
  5. Use the PersonalStorage.extract_message() method to obtain contact information in the MapiCalendar class.
  6. Use the MapiCalendar.save() method to save each calendar item to disk in ICS format.

The program below loads a PST file from disk and saves all calendar items in ICS format. These ICS files can then be used in any other program that supports standard ICS calendar files. When opened in Microsoft Outlook, an ICS file appears as shown in the screenshot below.

todo:image_alt_text

The following code snippet demonstrates how to export calendar items from an Outlook PST to ICS format:

Save ICS Files with Original Timestamps

The keep_original_date_time_stamp method of the MapiCalendarIcsSaveOptions class allows to preserve the original date and time stamps of the calendar items when saving them as an ICS (iCalendar) file. The following code sample demonstrates the implementation of this method:

import aspose.email as ae

pst = ae.storage.pst.PersonalStorage.from_file("my.pst")

calendar_folder = pst.get_predefined_folder(ae.storage.pst.StandardIpmFolder.APPOINTMENTS)

for msg_info in calendar_folder.enumerate_messages():
    cal = pst.extract_message(msg_info).to_mapi_message_item()

    save_options = ae.mapi.MapiCalendarIcsSaveOptions()
    save_options.keep_original_date_time_stamp = True

    if not (cal is None):
      cal.save("cal.ics", save_options)

Modify or Delete Recurrence Occurrences in PST Files

You can add exceptions to existing recurrence patterns or delete specific occurrences in PST files using the Aspose.Email for .NET API. The following code sample demonstrates how to make such modifications:

from datetime import datetime, timedelta
from aspose.email.storage.pst import PersonalStorage, StandardIpmFolder, FileFormatVersion
from aspose.email.mapi import MapiCalendar, MapiCalendarEventRecurrence, \
    MapiCalendarDailyRecurrencePattern, MapiCalendarRecurrenceEndType, \
    MapiCalendarExceptionInfo, MapiCalendarRecurrencePatternType, \
    MapiRecipientCollection, MapiRecipientType

start_date = datetime.now().date()

recurrence = MapiCalendarEventRecurrence()
pattern = MapiCalendarDailyRecurrencePattern()
pattern.pattern_type = MapiCalendarRecurrencePatternType.DAY
pattern.period = 1
pattern.end_type = MapiCalendarRecurrenceEndType.NEVER_END
recurrence.recurrence_pattern = pattern

exception_date = start_date + timedelta(days=1)

# adding one exception
exception_info = MapiCalendarExceptionInfo()
exception_info.location = "London"
exception_info.subject = "Subj"
exception_info.original_start_date = exception_date
exception_info.start_date_time = exception_date
exception_info.end_date_time = exception_date + timedelta(hours=5)
pattern.exceptions.append(exception_info)
pattern.modified_instance_dates.append(exception_date)
# every modified instance also has to have an entry in the DeletedInstanceDates field with the original instance date.
pattern.deleted_instance_dates.append(exception_date)

# adding one deleted instance
pattern.deleted_instance_dates.append(exception_date + timedelta(days=2))

rec_coll = MapiRecipientCollection()
rec_coll.add("receiver@domain.com", "receiver", MapiRecipientType.TO)
new_cal = MapiCalendar(
    "This is Location",
    "This is Summary",
    "This is recurrence test",
    start_date,
    start_date + timedelta(hours=3),
    "organizer@domain.com",
    rec_coll
)
new_cal.recurrence = recurrence

with PersonalStorage.create("output.pst", FileFormatVersion.UNICODE) as pst:
    calendar_folder = pst.create_predefined_folder("Calendar", StandardIpmFolder.APPOINTMENTS)
    calendar_folder.add_message(new_cal)