Coming in Mango–Query the Contacts List

Mango From Scratch

Coming in Mango is the ability to query the Contact List and retrieve considerably more email address information than was available previously.  In addition to the EmailAddress Chooser Task and the PhoneNumberChooserTask Mango will include an AddressChooserTask. 

In addition, Mango provides direct access to Contacts and Appointments. 

Note that these APIs are read only and that third part (e.g., Facebook) social network data cannot be accessed.

To get a sense of how the APIs work, let’s extract the Email Address and its type (personal/ work/ etc.) from the Contacts.  I created a Mango application that has a button and a list box.  The button handler does the work of accessing the names and email addresses and types and adds the list to the list box.

The constructor in my code-behind sets up the event handler for the button click.

The button click event handler creates a new instance of Contacts, sets the call back and then calls SearchAsync,

void Fetch_Click( object sender, RoutedEventArgs e )
    Contacts contacts = new Contacts();
    contacts.SearchCompleted += 
        new EventHandler<ContactsSearchEventArgs>( 
             contacts_SearchCompleted );
        null );


The three parameters to SearchAsync allow you to set up a filtered search.  FilterKindThe second of the three sets the kind of filter you want,  as shown in the illustration to the right.

What is particularly interesting here is that you can filter on just those addresses that are pinned to the start menu. 

We’ll return to filtered searches in an up-coming mini-tutorial.   For now we’re searching against all the contacts.

The call back handles the results of the search.  The user’s name is straight-forward, it is returned as the ToString of the Results property of the ContactsSearchEventArgs,

void contacts_SearchCompleted( 
   object sender, ContactsSearchEventArgs e )
    List<string> contacts = new List<string>();

    foreach (var result in e.Results)
        string name = result.ToString();


Email addresses are a bit trickier.  There may be none, one or many.  We’ll extract the first one if there is one and then check to make sure that it isn’t null. If not, we can use the email property to extract both the EmailAddress as well as the kind of EmailAddress we’ve retrieved (personal, work, etc.). 

We’ll then concatenate the user’s name with the email address and add all of that to the List<String> declared at the top of the method, finally assigning that list to the ItemsSource property of the list box.  Here’s the complete method,

void contacts_SearchCompleted( 
    object sender, ContactsSearchEventArgs e )
    List<string> contacts = new List<string>();

    foreach (var result in e.Results)
        string name = result.ToString();
        var email = result.EmailAddresses.FirstOrDefault();
        string contact = name;

        if (email != null)
            contact += " " + 
                email.EmailAddress.ToString() + " " + 
        contacts.Add( contact );
    Results.ItemsSource = contacts;

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen Pluralsight & LinkedIn Learning courses. He was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Xamarin Certified Mobile Developer and a Xamarin MVP and a Microsoft MVP.
This entry was posted in Data, Mango, Mini-Tutorial, WindowsPhone and tagged . Bookmark the permalink.

9 Responses to Coming in Mango–Query the Contacts List

  1. Andy B says:

    It seems to me that as an intermediate step the contact groups would be exposed in the API. As a dev, that would at least give me a way to work with contacts that are associated with my app. I havent been able to find the groups in the API yet. Am I missing it?

  2. Pingback: Coming in Mango–Query the Contacts List | WP7 Developers Links

  3. Anonymous says:

    I think SOME third-party data is available, just not ALL. So maybe double check that. And yes it is the licensing agreement that controls that. 🙂

  4. bob says:

    readonly access is unacceptable- there is no good way to move contacts from my iphone to the winphone and because you’ve kept contacts inaccessible, I can’t even code it.

    Why can’t we have a chooser that works like Outlook COM automation does- asks the user for permission and then lets us access it?

    This and the terrible mapping are the primary reasons I’m still carrying my iPhone.

    • Bob,
      I agree that read-only is inconvenient and prevents us from creating a number of applications that would have great utility. Unacceptable seems a bit strong. My understanding is that there were good reasons to defer allowing write to the contact list (see the Mix presentations) but that it may be coming in a future release.

      It is my experience that a lot of thought goes into these decisions, factoring in such things as the harm that might come from providing the capability, the time/cost to develop and the time to market, which are the highest priority features, and many other factors.

      That doesn’t mean that the dev team always gets it right, and I’ll pass along your frustration.

  5. Sam says:

    I think it’s a great feature, I was waiting for it. But it still a bit limited : accessing third part social network data would be nice.

  6. Michael says:

    Making the contacts read only is a little annoying. I guess that means we still can’t make an app that lets you simply manage the phone’s contacts? I’d like to be able to add/edit contacts, even if it prompts the user to confirm the change by way of a built-in dialog.

Comments are closed.