Add-in to Check for Double Resource Booking

I was working with a customer today that had an issue where one some occasions they were double-booking, triple booking or even booking up to 30 conference rooms for the same meeting. They were looking for a way to prevent this from happening because once booked and auto-accepted by the room, they were unable to “cancel” without an administrator. So, I wrote them some code that essentially does a check to make sure if they have more than one resource/room scheduled for a meeting it will prompt them to ask them if they are sure they want to do this. The trick – in this sample – is to put all the resources you care about into a text file called “MeetingRooms.txt” in the installation folder. It will read it into memory and then check to make sure no more than one is scheduled or it will stop the user from making the mistake.

You will need to start off creating a Visual Studio 2010 / Outlook 2010 / .NET 4.0 Add-in…Here is the code:

public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
// attach to the Item Send Event
Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
}

void Application_ItemSend(object Item, ref bool Cancel)
{
// try to cast to a Meeting Item
Outlook.MeetingItem meet = Item as Outlook.MeetingItem;
if (meet != null) // is it a meeting item - not null
{
// load the list of conference rooms from the install folder
List<string> rooms = new List<string>();
string path = System.Reflection.Assembly.GetExecutingAssembly().Location + "\\MeeingRooms.txt";
StreamReader sr = new StreamReader(path);
while (!sr.EndOfStream)
{
// save as lowercase -- we will compare this way
rooms.Add(sr.ReadLine().ToLower());
}

int cnt = 0;
// no loop through all the recipients
foreach (Outlook.Recipient r in meet.Recipients)
{
// if the recipient is in the List of rooms we count it
if (rooms.Contains(r.Name.ToLower()))
cnt++;

// now if we get more than one -- we have a problem
if (cnt > 1)
{
// notify the user they are about to send a meeting request
// that will book more than one room
DialogResult result = MessageBox.Show("You have more than one conference room on " +
"this meeting request. \n\n" +
"Are you sure you want to continue?", "Resource Conflict",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Hand);
// if the user hits cancel or no, we will
// cancel the send
if (result != DialogResult.Yes)
Cancel = true;
else
// otherwise, allow it
Cancel = false;

// exit for loop
break;
}
else
{
Cancel = false;
}
}
}
else
{
Cancel = false;
}
}

You need the following using statements:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;
using System.IO;