Hello,
wrote some code to export the whole calendar for each user in a distribution group as an ics File.
My problem is tha I am not able to export/write the Body of the appointment in the ics file. I get:
"You must load or assign this property before you can read its value.".
If I bind every appointment after I have searched every Calendarfolder to get
the Body it takes hours to generate the ics files.
Here is my code:
//Instantiate the ExchangeService class+ Exchange Certificate Validation + Impersonator ServicePointManager.ServerCertificateValidationCallback = CertificateValidationCallBack; ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2); service.Credentials = new NetworkCredential("user", "password", "domain"); service.AutodiscoverUrl("impersonator@domain", RedirectionUrlValidationCallback); //Distinguish Distribution Group string distributiongroup = "distribution@domain"; // Initialize values for the start and end time. DateTime startDate = DateTime.Now.AddDays(-10); DateTime endDate = startDate.AddDays(180); //Extend the distribution group ExpandGroupResults distGroupMembers = service.ExpandGroup(distributiongroup); foreach (EmailAddress address in distGroupMembers.Members) { //Impersonate each distribution group member service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, address.Address); //Create iCalendar and add local time zone iCalendar iCal = new iCalendar(); ITimeZone localTZ = iCal.AddLocalTimeZone(); // Build a list of additional time zones (local time zone is allready included) List<ITimeZone> otherTimeZones = new List<ITimeZone>(); foreach (TimeZoneInfo tzi in System.TimeZoneInfo.GetSystemTimeZones()) { if (tzi != System.TimeZoneInfo.Local) { // Add the time zone to our list (but don't include it directly in the calendar). otherTimeZones.Add(iCalTimeZone.FromSystemTimeZone(tzi)); } } //Path and Filename for ics File string icsFileDirectory = @"C:\\Users\\ikyriakidis\\Desktop\\ICAL\\"; string icsFileName1 = address.Address; string icsFileName2 = ".ics"; string icsFileName = icsFileName1 + icsFileName2; string icsFile = Path.Combine(icsFileDirectory, icsFileName); //Set reminder for the Meetings/Events (5 minutes befor event) Alarm alarm = new Alarm(); alarm.Action = AlarmAction.Display; alarm.Trigger = new Trigger(TimeSpan.FromMinutes(-05)); // Execute the search in the calendar folder and return the view CalendarView cView = new CalendarView(startDate, endDate); cView.PropertySet = new PropertySet(BasePropertySet.FirstClassProperties); FindItemsResults<Appointment> apt = service.FindAppointments(WellKnownFolderName.Calendar, cView); foreach (Appointment app in apt) { //Appointment appDetailed = Appointment.Bind(service, app.Id, new PropertySet(BasePropertySet.FirstClassProperties) { RequestedBodyType = BodyType.Text }); //Create event in ical Event iCalEvent = iCal.Create<Event>(); //Set the event papameters iCalEvent.Summary = app.Subject; //iCalEvent.Description = app.Subject; iCalEvent.Description = app.Body.Text; iCalEvent.Start = new iCalDateTime(app.Start); iCalEvent.End = new iCalDateTime(app.End); iCalEvent.Duration = app.Duration; iCalEvent.Location = app.Location; if (app.IsReminderSet == true) { alarm.Description = "Reminder"; iCalEvent.Alarms.Add(alarm); } if (app.Sensitivity == Sensitivity.Private) { iCalEvent.Summary = "privater Termin"; iCalEvent.Description = "privater Termin"; iCalEvent.Location = " "; } } //Create ics File iCalendarSerializer serializer = new iCalendarSerializer(iCal); if (!Directory.Exists(icsFileDirectory)) { Directory.CreateDirectory(icsFileDirectory); serializer.Serialize(icsFile); } else { serializer.Serialize(icsFile); } } }
Whene I replace the "iCalEvent.Description = app.Body.Text;" with "iCalEvent.Description = app.subject;" and comment out the "Appointment appDetailed = Appointment.Bind(service, app.Id, new PropertySet(BasePropertySet.FirstClassProperties) { RequestedBodyType = BodyType.Text });" it takes no more than 90 seconds to generate all the ics files!
Can you please help me on this one.. I have been strugeling for days to make it work...
Thank you.
Ioannis