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 );
    contacts.SearchAsync( 
        string.Empty, 
        FilterKind.None, 
        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() + " " + 
                email.Kind.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. Pingback: Coming in Mango–Query the Contacts List | WP7 Developers Links

Comments are closed.