Outlook C# Extensibility: Contacts Without a Photo

Gosh I love extensibility.  I'd so much rather take the time to write an app to automate a 1min task, if I had to do that task over and over again.  MS Office has great extensibility.  Here's a quick example of an app to list out all your contacts that don't have a photo on them yet.

The point of this is to show how you can very easily write C# code to pull custom reports from the Outlook data store.  I keep a photo collection of faces I crop from pictures and I like to have the photos on my contacts.  This tiny app just helps by letting me know which contacts need a pic.

I've got a good dozen or so custom Outlook apps, including apps to: custom filter mail, remove those pesky Appointment Reminders, create contacts from text files, count the amount of mail sent, create e-mail lists, export tasks to an XML file, create RSS feeds for mail, repair "Display As" contact fields, etc.

There are many types of Office extensibility apps, writing COM Addins, VBA in your Office file, using VSTO to write .NET code to tightly integrate within an office file (very cool), but this is just a simple example of writing a standalone .NET app to get data from Outlook.

Sample Code: OutlookContactsWithoutPhotos.zip

To use this example (with Visual Studio & Microsoft Outlook):

  1. Create a new console application project
  2. Change the project type (in the project properties) to a "Windows Application"
  3. Add a project references to the COM library "Microsoft Outlook x Control Library"
  4. Put this code in your app & run.

using System;

using System.Diagnostics;

using Outlook = Microsoft.Office.Interop.Outlook;

namespace OutlookContactsWithoutPhotos

{

   class Program

   {

      static void Main(string[] args)

      {

         // Obtain an instance of the Outlook application

         Outlook.Application app = new Outlook.ApplicationClass();

         // Access the MAPI namespace

         Outlook.NameSpace ns = app.GetNamespace("MAPI");

         // Get the user's default contacts folder

         Outlook.MAPIFolder contacts = ns.GetDefaultFolder(

            Outlook.OlDefaultFolders.olFolderContacts);

         // Iterate through each contact

         for (int i = 1; i < contacts.Items.Count + 1; i++)

         {

            // Get a contact

            Outlook.ContactItem contact =

               (Outlook.ContactItem)contacts.Items[i];

            // If this contact has a first name and no photo, list it

            if (contact.FirstName != null &&

               contact.FirstName.Trim().Length > 0 &&

               !contact.HasPicture) Trace.WriteLine(contact.FullName);

         }

      }

   }

}

References