Managing Calendar Items in PST Files
Add MAPI Calendar to PST
Creating and Managing PST Files shows 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:
- Create a MapiCalendar object.
- Set the MapiCalendar properties using a constructor and methods.
- Create a PST using the PersonalStorage.Create() method.
- 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 following code snippet shows you 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-.NET | |
string dataDir = RunExamples.GetDataDir_Outlook(); | |
// Create the appointment | |
MapiCalendar appointment = new MapiCalendar( | |
"LAKE ARGYLE WA 6743", | |
"Appointment", | |
"This is a very important meeting :)", | |
new DateTime(2012, 10, 2, 13, 0, 0), | |
new DateTime(2012, 10, 2, 14, 0, 0)); | |
// 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.", | |
new DateTime(2012, 10, 2, 13, 0, 0), | |
new DateTime(2012, 10, 2, 14, 0, 0), | |
"CharlieKhan@dayrep.com", | |
attendees | |
); | |
string path = dataDir + "AddMapiCalendarToPST_out.pst"; | |
if (File.Exists(path)) | |
{ | |
File.Delete(path); | |
} | |
using (PersonalStorage pst = PersonalStorage.Create(dataDir + "AddMapiCalendarToPST_out.pst", FileFormatVersion.Unicode)) | |
{ | |
FolderInfo calendarFolder = pst.CreatePredefinedFolder("Calendar", StandardIpmFolder.Appointments); | |
calendarFolder.AddMapiMessageItem(appointment); | |
calendarFolder.AddMapiMessageItem(meeting); | |
} |
Save Calendar Items 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. Use the PersonalStorage and MapiCalendar classes to get the calendar information. Below are the steps to save calendar items :
- Load the PST file in the PersonalStorage class.
- Browse the Calendar folder.
- Get the contents of the Calendar folder to get the message collection.
- Loop through the message collection.
- Call PersonalStorage.ExtractMessage() method to get the contact information in the MapiCalendar class.
- 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. Opened in Microsoft Outlook, an ICS file looks like the one in the below screenshot.
![]() |
---|
The following code snippet shows you how to export the calendar items from Outlook PST to ICS format.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir_Outlook(); | |
// Load the Outlook PST file | |
PersonalStorage pst = PersonalStorage.FromFile(dataDir + "Sub.pst"); | |
// Get the Calendar folder | |
FolderInfo folderInfo = pst.RootFolder.GetSubFolder("Inbox"); | |
// Loop through all the calendar items in this folder | |
MessageInfoCollection messageInfoCollection = folderInfo.GetContents(); | |
foreach (MessageInfo messageInfo in messageInfoCollection) | |
{ | |
// Get the calendar information | |
MapiMessage calendar = (MapiMessage)pst.ExtractMessage(messageInfo).ToMapiMessageItem(); | |
// Display some contents on screen | |
Console.WriteLine("Name: " + calendar.Subject); | |
// Save to disk in ICS format | |
calendar.Save(dataDir + @"\Calendar\" + calendar.Subject + "_out.ics"); | |
} |
Save with Original Timestamp
The following features are available to save calendar items as ICS preserving their original date and time information:
-
MapiCalendarIcsSaveOptions - Allows to specify additional options when saving MapiCalendar to Ics format.
-
MapiCalendarIcsSaveOptions.KeepOriginalDateTimeStamp - Allows keep original DateTimeStamp value in output file.
Use the code sample below to implement the features to your project:
var cal = pst.ExtractMessage(msgInfo).ToMapiMessageItem() as MapiCalendar;
if (cal != null)
{
cal.Save("cal.ics", new MapiCalendarIcsSaveOptions() { KeepOriginalDateTimeStamp = true});
}
Modify/Delete Occurrences from Recurrences
Exceptions can be added to existing recurrences using Aspose.Email for .NET API. The following code sample illustrates the usage of this feature.
// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET | |
DateTime startDate = DateTime.Now.Date.AddHours(12); | |
MapiCalendarEventRecurrence recurrence = new MapiCalendarEventRecurrence(); | |
MapiCalendarRecurrencePattern pattern = recurrence.RecurrencePattern = new MapiCalendarDailyRecurrencePattern(); | |
pattern.PatternType = MapiCalendarRecurrencePatternType.Day; | |
pattern.Period = 1; | |
pattern.EndType = MapiCalendarRecurrenceEndType.NeverEnd; | |
DateTime exceptionDate = startDate.AddDays(1); | |
// adding one exception | |
pattern.Exceptions.Add(new MapiCalendarExceptionInfo | |
{ | |
Location = "London", | |
Subject = "Subj", | |
OriginalStartDate = exceptionDate, | |
StartDateTime = exceptionDate, | |
EndDateTime = exceptionDate.AddHours(5) | |
}); | |
pattern.ModifiedInstanceDates.Add(exceptionDate); | |
// every modified instance also has to have an entry in the DeletedInstanceDates field with the original instance date. | |
pattern.DeletedInstanceDates.Add(exceptionDate); | |
// adding one deleted instance | |
pattern.DeletedInstanceDates.Add(exceptionDate.AddDays(2)); | |
MapiRecipientCollection recColl = new MapiRecipientCollection(); | |
recColl.Add("receiver@domain.com", "receiver", MapiRecipientType.MAPI_TO); | |
MapiCalendar newCal = new MapiCalendar( | |
"This is Location", | |
"This is Summary", | |
"This is recurrence test", | |
startDate, | |
startDate.AddHours(3), | |
"organizer@domain.com", | |
recColl); | |
newCal.Recurrence = recurrence; | |
using (MemoryStream memory = new MemoryStream()) | |
{ | |
PersonalStorage pst = PersonalStorage.Create(memory, FileFormatVersion.Unicode); | |
FolderInfo calendarFolder = pst.CreatePredefinedFolder("Calendar", StandardIpmFolder.Appointments); | |
calendarFolder.AddMapiMessageItem(newCal); | |
} |