Programmatically updating Outlook's address book options with a command line tool

I recently found the need to configure Outlook 2003 for thousands of corporate users to use preferred address book settings instead of the defaults. As anyone in a large corporate environment may know, the defaults are set such that when composing an email, name resolution checks all address lists in Exchange in alphabetical order by list, then name. When an organization is many tens of thousands large, resolving "Smith" will inevitably find conflicts.

[inline:addressing1.png]MAPI Schmappy
I recently found the need to configure Outlook 2003 for thousands of corporate users to use preferred address book settings instead of the defaults. As anyone in a large corporate environment may know, the defaults are set such that when composing an email, name resolution checks all address lists within the Global Address List alphabetically by list, then name. When an organization is many tens of thousands large, resolving "Smith" will inevitably find conflicts and irrelevant matches.

To ease the pain, it would be nice to resolve against the address list containing only names in your local organization before all others. This can be set manually in the Addressing dialog, found in Outlook address book's Tools->Options menu. Then, add the preferred address list to the top (see figure right).

You can also set the default address list, which defines which address list the Address Book will initially display and search upon. This should probably also be set to your local organization, instead of "All Contacts".

The Problem
There are no command line or scripting tools that do this for you. Searching Google rendered many people asking essentially the same questions but I wasn't able to find specific answers to this scenario. Even Microsoft Premier Support had no idea.

I found registry entries in HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles that define the profiles. Most values are REG_BINARY, which require much reverse engineering, and many of the keys are unique to each user/computer, so a simple registry import would not be possible in many cases.

It's clear we need an API to properly update this system.

MAPI
I've been able to solve this problem with MAPI in native C++. Microsoft documents MAPI in MSDN, but sadly it is not nearly as concise and complete as, say, the .NET framework components. But still, there is just enough information to get your feet wet and experiment.

Fortunately, MSDN did publish an example solution that did some of what I wanted. The code is pretty ugly, but it had the necessary steps to get started.

Using These Tools
Download the executables from the attachments below.

Setting Contact Name Resolution Search Path
Usage: MAPIAddrListSearch "Address List A" [ "Address List B" ...]

Setting Default Address List
Usage: MAPIDefaultAddressList "Address List"

For both tools, the address list names are recognized from what is available in the Addressing dialog. Be sure to enclose the names with quotes if the names contain spaces. If the tool can't resolve the name, it won't make the configuration change for that name.

It may be necessary to restart Outlook after running these tools. In some cases, it caches old settings and other times not.

Compiling From Source
I've provided the attached solution with source so that one day another sysadmin could find this useful. The source is provided as a Visual Studio solution with separate solution files for 2005 and 2008 editions.

Note: I have only tested this code on Outlook 2003. It is unknown how it will behave with prior versions or with Outlook 2007. Please contact me if you have feedback on other versions.

Deployment Method
For deployment to a large group, you have a few viable options. However, what you probably don't want to do is have this run in a login script and update the user's settings every time. It would be friendly to set a baseline, but allow the user to modify to their liking later. A quick way to do this is with a login script via GPO or AD user profile that runs once and creates a check file indicating the settings were set. Then, on successive logins to skip the configuration if the check file exists. This is demonstrated in the SetOutlookDefaults.cmd script included in the download.

Be aware that these tools modify an existing MAPI profile. If the user is logging in on a PC for the first time, or has never opened Outlook or other MAPI-based mail apps, there will be no MAPI profile yet.

AttachmentSize
MAPI Schmappy.zip129.85 KB
MAPI Schmappy source.zip16.41 KB

Comments

Worked on Outlook 2007

I tried this on Outlook 2007 and it worked great!

Here's the CMD file I used as a test:

:: Configure MAPI profile only once
	@echo off
	pushd "%~dp0"
	setlocal
 
:: If CHECKFLAG file already exists, skip configuration
	set CHECKFLAG=%temp%\%~n0.txt
	if exist "%CHECKFLAG%" goto :Exit
 
:: Configure MAPI profile, Abort if unsuccessful
	MAPIDefaultAddressList "All Users"
		if errorlevel 1 goto :Exit
	MAPIAddrListSearch "All Users" Contacts "Global Address List"
		if errorlevel 1 goto :Exit
 
:: Create CHECKFLAG file to indicate success
	echo. > "%CHECKFLAG%"
 
:Exit
endlocal
popd

Much appreciated!

Hi Shawn, I have been looking for way to accomplish this for literally years and so I when I saw it I was very jumping for joy. I gave the program a try and the search order part worked fine. But, the most important thing I wanted was the part that changes the default address book and this didn’t seem to work. I’m not a programmer at all so I don’t know how to troubleshoot this. I did use an accurate name but it didn’t work. I am using Outlook 2010 but I think the MAPI part is the same as older versions. Can you please please help me fix this? I have a situation that cannot be fixed without a program like this, so it’s a real life saver. Also, when I click the contact page on your site it says page not found. Please email me at dlee@customnas.com, thanks!

About Shawn Poulson / Exploding Coder
Software developer and designer

I'm a software developer in the Philadelphia/Baltimore metro area that loves web technology and creating compelling and useful applications. This blog was created to showcase some of my ideas and share my passion and experiences with others in the same field. Read more...

My portfolio...

Other places you can find me online: