Add a contact to Windows Mobile in C#

I have not posted much lately for one main reason: I like posting complete applications, or at least complete libraries. As it would have it, I do not have the time, energy, or creativity to post complete projects on a basis regular enough to warrant a site devoted to.

As such, I am going to try to start posting tips and snippets here and there between the major projects. I’ve had a number of new registrations to TCH recently and well, users need content. So…

Today I had a need for an automated way to add a contact to a Windows Mobile based device. One would think this is a fairly simple task. It is, however locating actual documentation as to the process to go about it is not so simple.

At this point I will assume you have the either the Windows Mobile 5 or 6 SDK installed and have created the appropriate Windows Mobile Application project. Keep in mind that unless you require .NET 2.0 features (which this example does not), be sure to select the 1.0 project to ensure support for WM5 based devices without the 2.0 Framework (I learned this the hard way).

The first step is to add a reference to Microsoft.WindowsMobile.PocketOutlook1  along with a using statement for the same. The Using statement is not required but will save some typing when using the POOM2 objects. This sample assumes you have added the using statement.

The Code

   1:  private void AddContact()
   2:  {
   3:    OutlookSession session = new OutlookSession();
   4:    ContactCollection contacts = session.Contacts.Items;
 
   5:    Contact contact = contacts.AddNew();
 
   6:    contact.FileAs = "John Doe";
   7:    contact.BusinessTelephoneNumber = "555-555-1212";
   8:    contact.Email1Address = "JohnDoe@isp.com";
   9:    contact.Update();
  10:  }
 
 

This is about the most basic way to add a contact. There are quite a few other properties of the Contact object that you are able to set than what’s pictured. For a descriptive list of all available properties, methods, and events visit this page.

In the example I use the FileAs property. When using FirstName & LastName, Outlook, both desktop and pocket versions, will by default display it in the format of Last, First. Using the FileAs property ensures it will display exactly as you write it.

No changes will be actually committed until the Update method is called.

That’s about all for now.

+++ATH0

  1. For anyone who has not had to do this, right click on the project in the Solution Explorer and select Add Reference.
  2. POOM stands for Pocket Outlook Object Model. It is similar to the desktop Outlook Object Model.

Leave a Reply