Fast and Simple Mobile Access to Pocket Outlook Data

For a new developer tool function to be useful, it must be easy to implement, provide a significant benefit to the task at hand, and perform well. As a Windows Mobile developer, I believe the addition of managed classes for accessing and interacting with the data contained within Pocket Outlook (contacts, tasks, and appointments) meets all these criteria.

Developers who build .NET Compact Framework applications for Pocket PCs and Smartphones must all-too-often provide access to Pocket Outlook data. Previously, this meant using P/Invoke to either call native DLLs’ functionalities or access internal data stores directly, or acquiring third-party managed code libraries. The result was increased development time and/or increased cost. With the new managed classes, C# and VB .NET Compact Framework developers can quickly gain access to this often-vital information and modify it (if necessary) without additional products.

The ABCs of Pocket Outlook Access

To utilize the new managed classes for Pocket Outlook, you need the following:

  • Visual Studio 2005
  • A Windows Mobile 5.0 device (Pocket PC, Pocket PC Phone Edition, or Smartphone)

The new managed classes are not available for Windows Mobile 2003 Second Edition or earlier devices. You can, however, use Visual Studio 2005 to develop either .NET Compact Framework 1.0 or 2.0 projects that take advantage of the new managed classes.

You need to perform only a few basic steps to access Pocket Outlook data from a Smart Device project in Visual Studio 2005:

  1. Add references to required assemblies.
  2. Create an instance variable for a Pocket Outlook session.
  3. Access the needed collection of information (appointments, tasks, contacts).
  4. Work with the collection.
  5. Dispose the Pocket Outlook session object.

By performing these steps, you can gain access to any information you need.

Adding References to Your Project

To gain access to the new managed classes for Pocket Outlook, you need to add references to two .NET Compact Framework assemblies to your project. You’ll find Microsoft.WindowsMobile.dll and Microsoft.WindowsMobile.PocketOutlook.dll in the Add References dialog, as shown in Figure 1.

Figure 1: Necessary References for Pocket Outlook Access

From a coding perspective, you access classes only in the Microsoft.WindowsMobile.PocketOutlook namespace. The other assembly is required, however, because the PocketOutlook assembly has dependencies on the first assembly for interfaces.

The PocketOutlook namespace contains a vast array of classes. Figure 2 shows just some of the available classes you’ll see in the Visual Studio Object Browser.

Figure 2: PocketOutlook Namespace Viewed in the Object Browser

Going forward, this article points out some of the more relevant classes to get you started.

Creating an OutlookSession Instance

For developers who have worked with the object model for Microsoft Outlook on the desktop, the Pocket Outlook object model should seem quite familiar. The object hierarchy and tasks associated with programming against this object model are very similar. One example of this is the initial programming step for accessing Pocket Outlook data: creating an instance of an Outlook session.

In the sample project, I created a single form containing a ListView control (named lvAppt) that I programmatically populate with Appointment information. Figure 3 shows this screen in design mode.

Figure 3: Appointments Screen in Design Mode

As you can see, I set up the ListView control into three columns for relevant appointment information. I use a ListViewItem object and its associated SubItems collection to populate this information.

To hold an instance of the PocketOutlook session, I created an instance variable in my form class named AppSession:

private OutlookSession AppSession;

For this demonstration, I added code to the form’s constructor (immediately following the call to the InitializeComponent method that creates the instance of the PocketOutlook session):

public Form1()
{
   InitializeComponent();

   //Create an instance of the Pocket Outlook session
   AppSession = new OutlookSession();

Accessing the Appointments Collection

The PocketOutlook object hierarchy provides simple access to collections of information. The collections are exposed through properties representing the folders containing associated items. They are accessed directly from the OutlookSession object. The most commonly used of these objects are the following:

  • Appointments (OutlookSession.Appointments)
  • Tasks (OutlookSession.Tasks)
  • Contacts (OutlookSession.Contacts)

Each of these categories also has associated collection objects in the forms of AppointmentCollection, TaskCollection, and ContactCollection. In my sample project, I need to access appointments. So, I created an instance variable to hold an Appointment collection (named AppAppts). To access the Appointments collection, I use the following code:

//Capture the AppointmentCollection into a variable
AppAppts = AppSession.Appointments.Items;

Once you have the collection, you can access individual items using the Appointment object. In my project, I used this object in conjunction with a foreach loop to walk through the collection:

//We now iterate through the entire collection with a foreach,
//using an appointment object...
foreach (Appointment appt in AppAppts)
{
   //Create a ListViewItem object
   lviAppt = new ListViewItem ();

   //Add the appointment date to the ListViewItem, and the time
   //and subject to ListViewItem SubItems
   lviAppt.Text = appt.Start.ToShortDateString();

   lviAppt.SubItems.Add(appt.Start.ToShortTimeString());
   lviAppt.SubItems.Add(appt.Subject);

   //Add the ListViewItem to the ListView
   lvAppts.Items.Add(lviAppt);
}

Note: The Task object for tasks and the Contact object for contacts can be used for this same purpose. Each of these objects provides programmatic access to all of the properties that are stored locally in the Pocket Outlook data stores. Figure 4 shows some of the properties associated with a Task object in the Object Browser.

Figure 4: Task Object Properties Displayed in the Object Browser

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read