Outlook add-in or Plugin development using C#.Net

This C#.Net tutorial, we will discuss Outlook add-in or Plugin development using C#.Net. We will see below examples:

  • Add menu in Outlook using C#.net in plugin application
  • Get Current Outlook version installed and Current configured Email ID in C#.Net
  • How to add menus in outlook menu bar using C#.Net
  • RDOItems GetActivitiesForTimeRange() function in outlook addin development C#.Net
  • Read all outlook calendar items using C#.net using outlook APIs
  • Read recurrence Pattern of a outlook meeting using redemption using C#.Net
  • Trap outlook calendar events in outlook meeting appointments in C#.Net

Add menu in Outlook using C#.net in plugin application

Now, we will see how to add a menu in outlook using C#.Net for plugin application.

For this you need to create C#.net outlook 2007/2010/2013 adding projects and here is a small brief by which you can create outlook add-in project.

From Visual Studio 2008 File -> New -> Project From the New Project Dialog box Expand Visual C# then Office then Click on 2007 and the select Outlook 2007 Add-in from the Visual Studio installed templates.

By doing this you will successfully able to create the Project. And in the add-in project, there will be a ThisAddIn.cs class. This class will be responsible for adding menus.

Here is the code to add menu in Outlook using C#.net in plugin application.

public partial class ThisAddIn
{
        private Office.CommandBarButton buttonHelp;
        private Office.CommandBarButton buttonFeedback;
        private Office.CommandBarButton buttonAbout;
        private string menuTag = “AspDotNetHelp”;
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
         //Search the menu and delete it if found
                    RemoveMenubar();
                    //Method to create new menu
                    AddMenuBar();
    }
          private void RemoveMenubar()
           {
            // If the menu already exists, remove it.
            try
            {
                Office.CommandBarPopup foundMenu = (Office.CommandBarPopup)
                    this.Application.ActiveExplorer().CommandBars.ActiveMenuBar.
                    FindControl(Office.MsoControlType.msoControlPopup,
                    missing, menuTag, true, true);
                if (foundMenu != null)
                {
                    foundMenu.Delete(true);
                }
            }
            catch (System.Exception ex)
            {
            }
         }

     private void AddMenuBar()
            {
    Account acc = new Account();
     menuBar = this.Application.ActiveExplorer().CommandBars.ActiveMenuBar;
                //Define the new Menu Bar into the old menu bar
                newMenuBar = (Office.CommandBarPopup)menuBar.Controls.Add(
                    Office.MsoControlType.msoControlPopup, missing,
                    missing, missing,true);
                //If I dont find the newMenuBar, I add it
                if (newMenuBar != null)
                {
                    newMenuBar.Caption = “AspDotNetHelp”;
                    newMenuBar.Tag = menuTag;
                    buttonAbout = (Office.CommandBarButton)newMenuBar.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, 1, true);
                    buttonAbout.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
                    buttonAbout.Caption = “About AspDotNetHelp”;
                    buttonAbout.Enabled = false;
                    // buttonAbout.FaceId = 3737;
                    //buttonAbout.Tag = “c1”;
                    buttonFeedback = (Office.CommandBarButton)newMenuBar.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, 1, true);
                    buttonFeedback.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
                    buttonFeedback.Caption = “Feedback”;
                    buttonFeedback.Enabled = false;
                    // buttonFeedback.FaceId = 3737;
                    // buttonFeedback.Tag = “c2”;
                    buttonHelp = (Office.CommandBarButton)newMenuBar.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, 1, true);
                    buttonHelp.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
                    buttonHelp.Caption = “Help”;
                    buttonHelp.Enabled = false;
                    // buttonHelp.FaceId = 3737;
                    //buttonHelp.Tag = “c3”;
 newMenuBar.Visible = true;
}
 menuBar.Visible = true;
//You can also add event handlers for the menus like
buttonHelp.Click += new Office._CommandBarButtonEvents_ClickEventHandler(buttonHelp_Click);
    }
}

If you will run this code then a new menu will appear help menu in Outlook.

Get Current Outlook version installed and Current configured Email ID in C#.net

Now, we will see how to get the Current Outlook version installed and Current configured Email ID in C#.net.

Here is the code to retrieve the current outlook version installed like outlook 2003 or Outlook 2007/2010 in C#.Net etc.

public static string GetOutlookVersion()
       {
           Microsoft.Office.Interop.Outlook.Application applicationObject = new
Microsoft.Office.Interop.Outlook.Application();
           return applicationObject.Version;
       }

The following code gives you the currently configured email id for outlook. There will be no issues whether it will be a POP3 account or it may be an exchange server account.

public static string GetCurrentUser()
       {
           string currentUser = string.Empty;
           try
           {
               RDOSessionClass session = new RDOSessionClass();
               session.Logon("", "", false, false, null, false);
               RDOAccounts accounts = session.Accounts;
               foreach (RDOAccount account in accounts)
               {
                   if (account.AccountType == rdoAccountType.atPOP3)
                   {
                       currentUser = session.CurrentUser.Address;
                   }
                   else if (account.AccountType == rdoAccountType.atExchange)
                   {
                       currentUser = session.CurrentUser.SMTPAddress;
                   }
               }
           }
           catch (System.Exception ex)
           {
           }
           return currentUser;
       }

This is how we can get Current Outlook version installed and Current configured Email ID in C#.net.

How to add menus in outlook menu bar using C#.net

Now, we will see how to add menus in the outlook menu bar using C#.Net. Here is the code to add menubar in outlook plugin application using C#.net.

private Office.CommandBar menuBar;
        private Office.CommandBarPopup newMenuBar;
        private Office.CommandBarButton buttonFeedback;
And in the ThisAddIn_Startup you can write the following lines of code that will show the menu  in the menu bar.
 menuBar = this.Application.ActiveExplorer().CommandBars.ActiveMenuBar;
    newMenuBar = (Office.CommandBarPopup)menuBar.Controls.Add(
                    Office.MsoControlType.msoControlPopup, missing,
                    missing, missing, true);
 if (newMenuBar != null)
                {
                    newMenuBar.Caption = “My&Menu”;
                    newMenuBar.Tag = menuTag;
 buttonFeedback = (Office.CommandBarButton)newMenuBar.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, 1, true);
                    buttonFeedback.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
                    buttonFeedback.Caption = “Customer &Feedback Option”;
//Face ID will use to show the ICON in the left side of the menu.
                    buttonFeedback.FaceId = 3737;
 newMenuBar.Visible = true;
}
//You can also add event handlers for the menus.
buttonFeedback.Click += new Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(buttonFeedback_Click);

This is how we can add menus in outlook menu bar using C#.Net.

RDOItems GetActivitiesForTimeRange() function in outlook addin development C#.Net

Now, we will see how to use GetActivitiesForTimeRange() function in outlook add-in development C#.Net.

GetActivitiesForTimeRange is a method that returns calendar meeting items between the specified date range in C#.Net API.

Here is a sample you can follow for reference.

RDOAppointmentItem mitem = null;
RDOSessionClass session = new RDOSessionClass();
               session.Logon(“”, “”, false, false, null, false);
               RDOFolder2 folder = (RDOFolder2)session.
GetDefaultFolder( rdoDefaultFolders. olFolderCalendar);
               RDOItems items = folder. GetActivitiesForTimeRange( StartDate.Date, StartDate.Date.AddDays(1), true);
               foreach (object item in items)
               {
                   mitem = item as RDOAppointmentItem;
          //You can also check whether the item is a recurring item or not.
                   if (mitem.IsRecurring)
                   {
                       //You can do whatever you want.
                   }
               }

This is how we can use RDOItems GetActivitiesForTimeRange() function in outlook add-in development C#.Net.

Read all outlook calendar items using C#.net using outlook APIs

Now, we will see how to read all outlook calendar items using C#.Net using outlook APIs. Here is the sample code to read all the outlook calendar items using c#.net.

using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;

Microsoft.Office.Interop.Outlook.Application app = null;
NameSpace ns = null;
private static Outlook.MAPIFolder calendar = null;
private Outlook.Items appointments = null;
app = this.Application;
ns = app.GetNamespace("mapi");
ns.Logon("", "", true, true);
calendar = ns.GetDefaultFolder( OlDefaultFolders. olFolderCalendar);
appointments = calendar.Items;

Read recurrence Pattern of a outlook meeting using redemption using C#.net

Now, we will discuss how to read recurrence Pattern of an outlook meeting using redemption using C#.net?

It’s really very difficult to read all the occurrences of a recurring meeting using any of the APIs available in the market. Sometimes if you will read through outlook APIs then it will not give the correct time if a user sends the meeting from different time zone.

I hope the below way somehow helps you to read the recurrence item from outlook meeting using C#.Net.

RDOSessionClass session = new RDOSessionClass();
                session.Logon("", "", false, false, null, false);
                RDOFolder folder = session.GetDefaultFolder(rdoDefaultFolders.olFolderCalendar);
                RDOItems items = folder.Items;
   foreach (object item in items)
                {
                    if (item is RDOAppointmentItem)
                    {
  RDOAppointmentItem rdoItem = item as RDOAppointmentItem;
  RDORecurrencePattern recPatt = rdoItem.GetRecurrencePattern();
                        int j = 1;
                        for (j = 1; j <= recPatt.Occurrences; j++)
                        {
//This loop will run for the no of ocuurance the recurring meeting contains.
                            RDOAppointmentItem rdoItem1 = null;
                            try
                            {
                                rdoItem1 = (RDOAppointmentItem)recPatt.GetOccurence(j);
                            }
                            catch (System.Exception ex)
                            {
                                MessageBox.Show(ex.InnerException.ToString());
                            }
                        }
}
}

If there will be no end date is selected in the recurring Pattern then the Occurrences count returns 0. But the most important thing is that it also gives the perfect time in case if the user changes the timezone.

Trap outlook calendar events in outlook meeting appointments in C#.Net

Now, we will discuss how to trap outlook calendar events in outlook meeting appointments in C#.Net.

If you are working in outlook plugin application development then we need to catch the events of the calendar folder. Here the requirement is to save meeting details into SQL Server 2008 database. But in case of a delete event, you will not get the GlobalAppointmentID of the meeting item.

Follow the below code to trap outlook calendar events in outlook meeting appointments in C#.Net.

using Office = Microsoft.Office.Core;
 public partial class ThisAddIn
{
        Microsoft.Office.Interop.Outlook.Application app = null;
        NameSpace ns = null;
        MAPIFolder calendar = null;
        Items appointments = null;
        private  Outlook.MAPIFolder DeletedItem;
        private  Outlook.Items itemDelete;
       private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            app = new Microsoft.Office.Interop.Outlook.Application();
            ns = app.GetNamespace("mapi");
            ns.Logon("", "", true, true);
            calendar = ns.GetDefaultFolder(OlDefaultFolders.olFolderCalendar);
            appointments = calendar.Items;
            appointments.ItemAdd += new ItemsEvents_ItemAddEventHandler(appointments_ItemAdd);
            appointments.ItemChange += new ItemsEvents_ItemChangeEventHandler(appointments_ItemChange);
            appointments.ItemRemove += new ItemsEvents_ItemRemoveEventHandler(appointments_ItemRemove);
       }
      void itemDelete_ItemChange(object Item)
        {
            AppointmentItem appointment = (Item as AppointmentItem);
            Messageox.Show(appointment.GlobalAppointmentID);
        }
        void appointments_ItemRemove()
        {
            DeletedItem = ns.GetDefaultFolder(OlDefaultFolders.olFolderDeletedItems);
            itemDelete = DeletedItem.Items;
            itemDelete.ItemChange += new ItemsEvents_ItemChangeEventHandler(itemDelete_ItemChange);
        }
        void appointments_ItemChange(object Item)
        {
            AppointmentItem appointment = (Item as AppointmentItem);
            Messageox.Show(appointment.GlobalAppointmentID);
        }
        void appointments_ItemAdd(object Item)
        {
           AppointmentItem appointment = (Item as AppointmentItem);
            Messageox.Show(appointment.GlobalAppointmentID);
        }
}

This is how we can trap outlook calendar events in outlook meeting appointments in C#.Net.

You may like following C#.Net tutorials:

This C#.Net tutorial, we learned Outlook add-in or Plugin development using C#.Net. We learned below examples:

  • Add menu in Outlook using C#.net in plugin application
  • Get Current Outlook version installed and Current configured Email ID in C#.net
  • How to add menus in outlook menu bar using C#.net
  • RDOItems GetActivitiesForTimeRange() function in outlook add-in development C#.Net
  • Read all outlook calendar items using C#.net using outlook APIs
  • Read recurrence Pattern of an outlook meeting using redemption using C#.net
  • Trap outlook calendar events in outlook meeting appointments in C#.Net
Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>