locked
C#: Get Event from shared calendar(s) RRS feed

  • Question

  • Hello to you all,

    I have to retreive all my shared calendar's event for the current day. I am trying to do this in C#. I have tried with this code:

    Outlook.Application oApp = new Outlook.Application();
    
    
    
    
    
    Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
    
    
    
      
    
    oNS.Logon(Missing.Value, Missing.Value, true, true);
    
    
    
        
    
    // Get the Calendar folder.
    
    Outlook.MAPIFolder oCalendar = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
    
        
    
    // Get the Items (Appointments) collection from the Calendar folder.
    
    Outlook.Items oItems = oCalendar.Items;
    
    

    Unfortunately, I get only event from my calendar, and nothing from the shared ones....where did I go wrong? What am I missing?

    Many thanks

    Thursday, November 25, 2010 8:12 PM

Answers

  • Hi Pharaday,

    Welcome to MSDN Forums!

     

    using System;
    using Outlook = Microsoft.Office.Interop.Outlook;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          Outlook.Application objOLApp;
          Outlook.MAPIFolder objFolder;
          Outlook.Explorer objExplorer;
          Outlook.MAPIFolder objSubFolder;
          Outlook.AppointmentItem objCalenderItem;
          Outlook.Folders objOutlookFolders;
          int intFolderCtr;
          int intSubFolderCtr;
          int intAppointmentCtr;
          // >> Initialize The Base Objects
          objOLApp = new Outlook.Application();
          objOutlookFolders = objOLApp.Session.Folders;
          // >> Loop Through The PST Files Added n Outlook
          for (intFolderCtr = 1; intFolderCtr <= objOutlookFolders.Count; intFolderCtr++)
          {
            objFolder = objOutlookFolders[intFolderCtr];
            objExplorer = objFolder.GetExplorer();
            // >> Loop Through The Folders In The PST File
            for (intSubFolderCtr = 1; intSubFolderCtr <= objExplorer.CurrentFolder.Folders.Count; intSubFolderCtr++)
            {
              objSubFolder = objExplorer.CurrentFolder.Folders[intSubFolderCtr];
              // >> Check if Folder Contains Appointment Items
              if (objSubFolder.DefaultItemType == Outlook.OlItemType.olAppointmentItem)
              {
                // >> Loop Through Appointment Items
                for (intAppointmentCtr = 1; intAppointmentCtr <= objSubFolder.Items.Count; intAppointmentCtr++)
                {
                  // >> Get Teh Calender Item From The Calender Folder
                  objCalenderItem = objSubFolder.Items[intAppointmentCtr];
                  // >> Process Appointment Item Accordingly
                  Console.WriteLine(objCalenderItem.Subject + ", " + objCalenderItem.Location);
                }
              }
            }
          }
          // >> Close Application
          objOLApp.Quit();
          // >> Release COM Object
          System.Runtime.InteropServices.Marshal.ReleaseComObject(objOLApp);
          objOLApp = null;
          Console.ReadLine();
        }
      }
    }
    
    

    If there's anything unclear or there's anything I had misunderstood you, please feel free to let me know.

    Have a nice day!

    Mike

    ----------

    If(helpful) -> Mark

    *********************

    Welcome to the new world! [All-In-One Code Framework]

    Thursday, December 2, 2010 4:48 PM
  • The shared calendars are seperate folders, getting the default calendar gets the calendar for the user you logged on as!

    You need to iterate through all the folders determine thier type and interegate each one for entries.

    See this link for the answer http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/83af3946-6ab7-465b-bc06-7c89d8fa1143

    Regards

    Rupert

     


    the problem is not what you don't know it's what you think you know that's wrong
    Saturday, November 27, 2010 3:18 PM

All replies

  • The shared calendars are seperate folders, getting the default calendar gets the calendar for the user you logged on as!

    You need to iterate through all the folders determine thier type and interegate each one for entries.

    See this link for the answer http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/83af3946-6ab7-465b-bc06-7c89d8fa1143

    Regards

    Rupert

     


    the problem is not what you don't know it's what you think you know that's wrong
    Saturday, November 27, 2010 3:18 PM
  • Hello,

    I've tried the answer showed in the link but it is not working in c#. Here is the c# code that I took from the vb's code showed in the link. If you see what is wrong, please let me know:

    try
          {
            int intFolderCtr;
            int intSubFolderCtr;
            int intAppointmentCtr;
            Outlook.Application oApp = new Outlook.Application();
            Outlook.MAPIFolder objFolder;
            Outlook.Explorer objExplorer;
            Outlook.MAPIFolder objSubFolder;
            Outlook.AppointmentItem objCalenderItem;
            Outlook.Folders objOutlookFolders;
                    
    
            objOutlookFolders = oApp.Session.Folders;
            
            //loop trough folders
            for (intFolderCtr = 0; intFolderCtr < objOutlookFolders.Count; intFolderCtr = intFolderCtr + 1)
            {
              
              if (intFolderCtr == 0)
              {
                objFolder = objOutlookFolders.GetFirst();
              }
              else
              {
                objFolder = objOutlookFolders.GetNext();
              }
              
              
              //objExplorer = objFolder.GetExplorer(Outlook.OlFolderDisplayMode.olFolderDisplayNormal);
              
              
              //iterate trough sub folder
              //for (intSubFolderCtr = 0; intSubFolderCtr < objExplorer.CurrentFolder.Folders.Count; intSubFolderCtr = intSubFolderCtr + 1)
              for (intSubFolderCtr = 0; intSubFolderCtr < objFolder.Folders.Count; intSubFolderCtr = intSubFolderCtr + 1)
              {
                if (intSubFolderCtr == 0)
                {
                  objSubFolder = objFolder.Folders.GetFirst();
                }
                else
                {
                  objSubFolder = objFolder.Folders.GetNext();
                }
    
                
                //check if appointement in folder           
                if (objSubFolder.DefaultItemType == Outlook.OlItemType.olAppointmentItem)
                {
                  
                  //loop trough appointements type
                  for (intAppointmentCtr = 0; intAppointmentCtr < objSubFolder.Items.Count; intAppointmentCtr = intAppointmentCtr + 1)
                  {
    
    
                    //get the calendar item from the calendar folder                
                    if (intAppointmentCtr == 0)
                    {
                      objCalenderItem = (Outlook.AppointmentItem)objSubFolder.Items.GetFirst();
                    }
                    else
                    {
                      objCalenderItem = (Outlook.AppointmentItem)objSubFolder.Items.GetNext();
                    }
    
                    
                    objCalenderItem.Display(Outlook.OlFolderDisplayMode.olFolderDisplayNoNavigation);
    
    
                  }//iterates trough appointement items
    
    
                }//            
    
              }//
    
                
    
    
            }//end main for loop
    
    
            //close and release COM object
            //oApp.Quit();
            oApp = null;
            System.Runtime.InteropServices.Marshal.ReleaseComObject(oApp);
    
          }
    
          //Simple error handling.
          catch (Exception e)
          {
            Console.WriteLine("{0} Exception caught.", e);
          }
    Many thanks
    Monday, November 29, 2010 8:34 PM
  • Hi Pharaday,

    Welcome to MSDN Forums!

     

    using System;
    using Outlook = Microsoft.Office.Interop.Outlook;
    
    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          Outlook.Application objOLApp;
          Outlook.MAPIFolder objFolder;
          Outlook.Explorer objExplorer;
          Outlook.MAPIFolder objSubFolder;
          Outlook.AppointmentItem objCalenderItem;
          Outlook.Folders objOutlookFolders;
          int intFolderCtr;
          int intSubFolderCtr;
          int intAppointmentCtr;
          // >> Initialize The Base Objects
          objOLApp = new Outlook.Application();
          objOutlookFolders = objOLApp.Session.Folders;
          // >> Loop Through The PST Files Added n Outlook
          for (intFolderCtr = 1; intFolderCtr <= objOutlookFolders.Count; intFolderCtr++)
          {
            objFolder = objOutlookFolders[intFolderCtr];
            objExplorer = objFolder.GetExplorer();
            // >> Loop Through The Folders In The PST File
            for (intSubFolderCtr = 1; intSubFolderCtr <= objExplorer.CurrentFolder.Folders.Count; intSubFolderCtr++)
            {
              objSubFolder = objExplorer.CurrentFolder.Folders[intSubFolderCtr];
              // >> Check if Folder Contains Appointment Items
              if (objSubFolder.DefaultItemType == Outlook.OlItemType.olAppointmentItem)
              {
                // >> Loop Through Appointment Items
                for (intAppointmentCtr = 1; intAppointmentCtr <= objSubFolder.Items.Count; intAppointmentCtr++)
                {
                  // >> Get Teh Calender Item From The Calender Folder
                  objCalenderItem = objSubFolder.Items[intAppointmentCtr];
                  // >> Process Appointment Item Accordingly
                  Console.WriteLine(objCalenderItem.Subject + ", " + objCalenderItem.Location);
                }
              }
            }
          }
          // >> Close Application
          objOLApp.Quit();
          // >> Release COM Object
          System.Runtime.InteropServices.Marshal.ReleaseComObject(objOLApp);
          objOLApp = null;
          Console.ReadLine();
        }
      }
    }
    
    

    If there's anything unclear or there's anything I had misunderstood you, please feel free to let me know.

    Have a nice day!

    Mike

    ----------

    If(helpful) -> Mark

    *********************

    Welcome to the new world! [All-In-One Code Framework]

    Thursday, December 2, 2010 4:48 PM
  • Hi,

    Many thanks for your answer. It was very helpfull. The only thing missing is when I try to access the appointment item's body, Outlook display a message box asking for granting permission to a third party trying to access Outlook's content. How can I resolve that? Because when I want the subject, starting or ending date I do not have this problem.

    Many thanks,

     

    Wednesday, December 15, 2010 2:23 PM
  • Hi  Pharaday,

     

    I'm not a expert on office development, and we have a special forum for discuss the office develop problem.

    The following is the forum address, and you can create thread in that forum. There're more office develop experts will give you the expert help.

    http://social.msdn.microsoft.com/Forums/en-US/outlookdev/threads

     

    Best wishes,


    Mike [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, December 16, 2010 9:00 AM