none
How to transfer Navigation Folder content to MAPI Folder RRS feed

  • Question

  • Hi 

    I have been trying to access a shared calendar appointments from outlook programatically. But I am able to get to the Shared Calendar but not sure how to read the appointments from the Folder. I tried to explicitely assign the Navigation Folder contents to MAPI but getting error.

    Can someone suggest?

    static void Main(string[] args) { Outlook.Application oApp = new Outlook.Application(); Outlook.NameSpace oNS = oApp.GetNamespace("MAPI"); try { Outlook.Folder oCtFolder = (Outlook.Folder)oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar); CalendarModule calModule = (CalendarModule)oApp.ActiveExplorer().NavigationPane.Modules.GetNavigationModule(OlNavigationModuleType.olModuleCalendar); Microsoft.Office.Interop.Outlook.Folder ofldr = null; Outlook.NavigationFolder navFolder = null; Outlook.Folder olFolder = null; Outlook.Items OtherFolderItems = null; foreach (NavigationGroup group in calModule.NavigationGroups) { if ("Shared Calendars".Equals(group.Name)) { for (int i = 1; i <= group.NavigationFolders.Count; i++) { NavigationFolder olNavigationFolder = group.NavigationFolders[i]; Console.WriteLine("{0} ", olNavigationFolder.DisplayName);

    // ERROR in the below line Microsoft.Office.Interop.Outlook.MAPIFolder oCalendar = (Microsoft.Office.Interop.Outlook.MAPIFolder)olNavigationFolder; // Error Microsoft.Office.Interop.Outlook.Items oItems = olNavigationFolder.Folder.Items; foreach (Microsoft.Office.Interop.Outlook.AppointmentItem oAppt in oItems) { Console.WriteLine("Subject: " + oAppt.Subject); Console.WriteLine("Organizer: " + oAppt.Organizer); } //Console.WriteLine("olNavigationFolder.Class {0}", olNavigationFolder.Class); MAPIFolder mapiFolder = olNavigationFolder.Folder; foreach (Items itm in mapiFolder.Items) { Console.WriteLine(" itm : {0}", itm.Class); } } } } Console.ReadKey(); } finally { //Marshal.FinalReleaseComObject(oItem); //oItem = null; oNS.Logoff(); //((Outlook._Application)oApp).Quit(); } }


    Wednesday, September 19, 2018 9:06 PM

Answers

  • Atually just tried something like this and it worked for me. Here is the complete code.

    I used the below code to fetch the Navigation folder items and it worked like charm !

    Microsoft.Office.Interop.Outlook.Items oItems = olNavigationFolder.Folder.Items;
    

    //COMPLETE CODE

    static void Main(string[] args)
            {
                Outlook.Application oApp = new Outlook.Application();
                Outlook.NameSpace oNS = oApp.GetNamespace("MAPI");
                try
                {
                   // Outlook.Folder oCtFolder = (Outlook.Folder)oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
                    CalendarModule calModule = (CalendarModule)oApp.ActiveExplorer().NavigationPane.Modules.GetNavigationModule(OlNavigationModuleType.olModuleCalendar);
                    foreach (NavigationGroup group in calModule.NavigationGroups)
                    {
                        if ("Shared Calendars".Equals(group.Name))
                        {
                            for (int i = 1; i <= group.NavigationFolders.Count; i++)
                            {
                               NavigationFolder olNavigationFolder = group.NavigationFolders[i];
                               Console.WriteLine("******************************************************************");
                               Console.WriteLine("{0} ", olNavigationFolder.DisplayName);
                               Console.WriteLine(olNavigationFolder.Folder.Items.Count.ToString());
                               Microsoft.Office.Interop.Outlook.Items oItems = olNavigationFolder.Folder.Items;
    
                                foreach (Microsoft.Office.Interop.Outlook.AppointmentItem oAppt in oItems)
                               {
                                   Console.WriteLine("Appointment ID: " + oAppt.GlobalAppointmentID);
                                   Console.WriteLine("Subject: " + oAppt.Subject);
                                   Console.WriteLine("Organizer: " + oAppt.Organizer);
                                   Console.WriteLine("Start: " + oAppt.Start);
                                   Console.WriteLine("End: " + oAppt.End);
                                   Console.WriteLine("Location: " + oAppt.Location);
                                   Console.WriteLine("Duration: " + oAppt.Duration);
                                   Console.WriteLine("IsRecurring: " + oAppt.IsRecurring);
                                   Console.WriteLine("LastModificationTime: " + oAppt.LastModificationTime);
                                   Console.WriteLine("AllDayEvent: " + oAppt.AllDayEvent);
                                   Console.WriteLine("\n\n");
                                   //Console.ReadKey();
                               }                           
                            }
                        }
    
                    }
                   // Console.ReadKey();
                }
                finally
                {
                    //Marshal.FinalReleaseComObject(oItem);
                    //oItem = null; 
                    oNS.Logoff();
                    //((Outlook._Application)oApp).Quit();
                }
            }



    Thursday, September 20, 2018 3:46 AM

All replies

  • I've tested your code, you should convert olNavigationFolder to Outlook.MAPIFolder type

    var obj = olNavigationFolder as Outlook.MAPIFolder;

    You could change your code to the following code:

    static void Main(string[] args)
            {
                Outlook.Application oApp = new Outlook.Application();
                Outlook.NameSpace oNS = oApp.GetNamespace("MAPI");
    
                try
                {
    
                    Outlook.Folder oCtFolder = (Outlook.Folder)oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
                    CalendarModule calModule = (CalendarModule)oApp.ActiveExplorer().NavigationPane.Modules.GetNavigationModule(OlNavigationModuleType.olModuleCalendar);
                    Microsoft.Office.Interop.Outlook.Folder ofldr = null;
    
                    Outlook.NavigationFolder navFolder = null;
                    Outlook.Folder olFolder = null;
                    Outlook.Items OtherFolderItems = null;
                    Console.WriteLine(calModule.NavigationGroups);
                    foreach (NavigationGroup group in calModule.NavigationGroups)
                    {
                        Console.WriteLine(group.Name);
                        if ("Shared Calendars".Equals(group.Name))
                        {
                            for (int i = 1; i <= group.NavigationFolders.Count; i++)
                            {
                                NavigationFolder olNavigationFolder = group.NavigationFolders[i];
                                Console.WriteLine(olNavigationFolder);
                                var obj = olNavigationFolder as Outlook.MAPIFolder;
                                //Console.WriteLine("{0} ", obj.DisplayName);
                                // ERROR in the below line
                                Microsoft.Office.Interop.Outlook.MAPIFolder oCalendar = (Microsoft.Office.Interop.Outlook.MAPIFolder)obj;  // Error
                                Microsoft.Office.Interop.Outlook.Items oItems = olNavigationFolder.Folder.Items;
                                foreach (Microsoft.Office.Interop.Outlook.AppointmentItem oAppt in oItems)
                                 {
                                  Console.WriteLine("Subject: " + oAppt.Subject);
                                   Console.WriteLine("Organizer: " + oAppt.Organizer);
                               }
    
                                Console.WriteLine("olNavigationFolder.Class {0}", olNavigationFolder.Class);
                                Console.WriteLine(olNavigationFolder.Folder);
                                Console.WriteLine(olNavigationFolder.Folder.Items);
                                MAPIFolder mapiFolder = olNavigationFolder.Folder;
                                Microsoft.Office.Interop.Outlook.Items oItemsmapi = mapiFolder.Items;
                                foreach (Microsoft.Office.Interop.Outlook.AppointmentItem oApptmapi in oItemsmapi)
                                {
                                    Console.WriteLine(" oApptmapi : {0}", oApptmapi.Class);
                                }
    
                            }
                        }
    
                    }
    
                    Console.ReadKey();
                }
                finally
                {
                    //Marshal.FinalReleaseComObject(oItem);
                    //oItem = null; 
                    oNS.Logoff();
                    //((Outlook._Application)oApp).Quit();
                }
            }


    Please remember to click &quot;Mark as Answer&quot; the responses that resolved your issue, and to click &quot;Unmark as Answer&quot; if not. This can be beneficial to other community members reading this thread.

    Thursday, September 20, 2018 2:48 AM
  • Atually just tried something like this and it worked for me. Here is the complete code.

    I used the below code to fetch the Navigation folder items and it worked like charm !

    Microsoft.Office.Interop.Outlook.Items oItems = olNavigationFolder.Folder.Items;
    

    //COMPLETE CODE

    static void Main(string[] args)
            {
                Outlook.Application oApp = new Outlook.Application();
                Outlook.NameSpace oNS = oApp.GetNamespace("MAPI");
                try
                {
                   // Outlook.Folder oCtFolder = (Outlook.Folder)oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
                    CalendarModule calModule = (CalendarModule)oApp.ActiveExplorer().NavigationPane.Modules.GetNavigationModule(OlNavigationModuleType.olModuleCalendar);
                    foreach (NavigationGroup group in calModule.NavigationGroups)
                    {
                        if ("Shared Calendars".Equals(group.Name))
                        {
                            for (int i = 1; i <= group.NavigationFolders.Count; i++)
                            {
                               NavigationFolder olNavigationFolder = group.NavigationFolders[i];
                               Console.WriteLine("******************************************************************");
                               Console.WriteLine("{0} ", olNavigationFolder.DisplayName);
                               Console.WriteLine(olNavigationFolder.Folder.Items.Count.ToString());
                               Microsoft.Office.Interop.Outlook.Items oItems = olNavigationFolder.Folder.Items;
    
                                foreach (Microsoft.Office.Interop.Outlook.AppointmentItem oAppt in oItems)
                               {
                                   Console.WriteLine("Appointment ID: " + oAppt.GlobalAppointmentID);
                                   Console.WriteLine("Subject: " + oAppt.Subject);
                                   Console.WriteLine("Organizer: " + oAppt.Organizer);
                                   Console.WriteLine("Start: " + oAppt.Start);
                                   Console.WriteLine("End: " + oAppt.End);
                                   Console.WriteLine("Location: " + oAppt.Location);
                                   Console.WriteLine("Duration: " + oAppt.Duration);
                                   Console.WriteLine("IsRecurring: " + oAppt.IsRecurring);
                                   Console.WriteLine("LastModificationTime: " + oAppt.LastModificationTime);
                                   Console.WriteLine("AllDayEvent: " + oAppt.AllDayEvent);
                                   Console.WriteLine("\n\n");
                                   //Console.ReadKey();
                               }                           
                            }
                        }
    
                    }
                   // Console.ReadKey();
                }
                finally
                {
                    //Marshal.FinalReleaseComObject(oItem);
                    //oItem = null; 
                    oNS.Logoff();
                    //((Outlook._Application)oApp).Quit();
                }
            }



    Thursday, September 20, 2018 3:46 AM
  • Thanks for sharing this. Please remember to mark your reply as answer if it helped. Please help us close the thread. Thank you for understanding! If you have any question, or update, please feel free to let me know.

    Regards

    Alina


    Please remember to click &quot;Mark as Answer&quot; the responses that resolved your issue, and to click &quot;Unmark as Answer&quot; if not. This can be beneficial to other community members reading this thread.


    Thursday, September 20, 2018 6:05 AM