1

I am building an application that accesses meetings from Exchange. I'm using the code provided by Microsoft in their EWS documentation. The issue is I need to access a specific calendar. Say, I create 2 calendars apart from the default one. When I access meetings using this code, I only get meetings from the default calendar. I want to access meetings from a specific calendar. How can I do this?

Thanks for the help.

 // Initialize values for the start and end times, and the number of appointments to retrieve.
            DateTime startDate = DateTime.Now;
            DateTime endDate = startDate.AddDays(30);
            const int NUM_APPTS = 5;

            // Initialize the calendar folder object with only the folder ID. 
            CalendarFolder calendar = CalendarFolder.Bind(service, WellKnownFolderName.Calendar, new PropertySet());

            // Set the start and end time and number of appointments to retrieve.
            CalendarView cView = new CalendarView(startDate, endDate, NUM_APPTS);

            // Limit the properties returned to the appointment's subject, start time, and end time.
            cView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End);

            // Retrieve a collection of appointments by using the calendar view.
            FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView);

            Console.WriteLine("\nThe first " + NUM_APPTS + " appointments on your calendar from " + startDate.Date.ToShortDateString() + 
                              " to " + endDate.Date.ToShortDateString() + " are: \n");
            
            foreach (Appointment a in appointments)
            {
                Console.Write("Subject: " + a.Subject.ToString() + " ");
                Console.Write("Start: " + a.Start.ToString() + " ");
                Console.Write("End: " + a.End.ToString());
                Console.WriteLine();
            }

1 Answers1

0

I think your problem is that you are using WellKnownFolderName.Calendar:

CalendarFolder calendar = CalendarFolder.Bind(service, WellKnownFolderName.Calendar, new PropertySet());

Instead you should use the FolderId of the calendar you have created. To get the id of the folder (calendar) you can use the code similar to this (found in answer: https://stackoverflow.com/a/24133821/1037864)

    ExtendedPropertyDefinition PR_Folder_Path = new ExtendedPropertyDefinition(26293, MapiPropertyType.String);
    PropertySet psPropSet = new PropertySet(BasePropertySet.FirstClassProperties);
    psPropSet.Add(PR_Folder_Path);
    FolderId rfRootFolderid = new FolderId(WellKnownFolderName.Root, mbMailboxname);
    FolderView fvFolderView = new FolderView(1000);
    fvFolderView.Traversal = FolderTraversal.Deep;
    fvFolderView.PropertySet = psPropSet;
    SearchFilter sfSearchFilter = new SearchFilter.IsEqualTo(FolderSchema.FolderClass, "IPF.Appointment");
    FindFoldersResults ffoldres = service.FindFolders(rfRootFolderid, sfSearchFilter, fvFolderView);
    if (ffoldres.Folders.Count > 0) {
        foreach (Folder fld in ffoldres.Folders) {
            Console.WriteLine(fld.Id.ToString() + " " + fld.DisplayName);
        }
    }

Take the value from fld.Id and use instead of WellKnownFolderName.Calendar.

(I don't have an Exchange server available right now to try this out but I hope you get the idea and that my code i correct.)

Community
  • 1
  • 1
Björn
  • 3,098
  • 2
  • 26
  • 40