Click here to Skip to main content
Click here to Skip to main content

How to Retrieve Contact Information in WP7 using the AddressChooserTask?

, 31 Mar 2012
Rate this:
Please Sign up or sign in to vote.
In this small post we will learn how to retrieve saved contacts in WP7 using the Windows Phone 7 SDK APIs.

In the last post, we learnt “How to Save Contact in WP7 using the SaveContactTask?” Today in this small post we will learn how to retrieve saved contacts in WP7 using the Windows Phone 7 SDK APIs.

If you are developing any application for Windows Phone 7 and wants to provide user an option to get any contact details (e.g., Contact Name, complete Address of the Contact), AddressChooserTask class will allow you to get such information of the selected contact. Continue reading to get more details about this API.

Know About the API

AddressChooserTask is a sealed class present in the Microsoft.Phone.Tasks namespace. It derived from ChooserBase of type AddressResult. The class consists of only one method named Show() which will launch the Contact Chooser screen where user will be able to select the desired contact.

Here is the meta data of the AddressChooserTask:

namespace Microsoft.Phone.Tasks
{
    public sealed class AddressChooserTask : ChooserBase<addressresult>
    {
        public override void Show();
    }
}</addressresult>

AddressChooserTask has a Completed event, where it will get the AddressResult as selected item. AddressResult class is also a sealed class present in the Microsoft.Phone.Tasks namespace and exposes two string properties named DisplayName and Address. DisplayName returns the name of the contact and the Address returns the complete address of the selected contact item.

Here is the meta data of the AddressResult class:

namespace Microsoft.Phone.Tasks
{
    public sealed class AddressResult : TaskEventArgs
    {
        public AddressResult();
        public AddressResult(TaskResult taskResult);

        public string Address { get; internal set; }
        public string DisplayName { get; internal set; }
    }
}

Implementation Steps

It is very easier to implement the address chooser task. Just create the instance of the class and register the Completed event. Finally call the Show() method which will display the specific UI in the screen.

// Create the instance of the AddressChooserTask
var addressChooserTask = new AddressChooserTask();

// Register for the Completed event
addressChooserTask.Completed += AddressChooserTask_Completed;

// Call the Show method to open the Contact List Screen
addressChooserTask.Show();

In the Completed event implementation, you can check the status of the task. If user chose a contact from the contact list, the TaskResult will come as "OK" and if the user cancels the selection process or interrupts the task, it will return as "Cancel".

Here is the simplest way of implementation of the AddressChooserTask_Completed event:

void AddressChooserTask_Completed(object sender, AddressResult e)
{
    switch (e.TaskResult)
    {
        case TaskResult.OK:
            // Fetch the selected Contact
            MessageBox.Show(e.DisplayName + "\n\n" + e.Address);
            break;

        case TaskResult.Cancel:
            // Contact Selection Operation Cancelled
            MessageBox.Show("Address Chooser Task interrupted by the user");
            break;

        default:
            break;
    }
}

Here is the screenshot of what you will see:

Image 1: How to Retrieve Contact Information in WP7 using the AddressChooserTask? Image 2: How to Retrieve Contact Information in WP7 using the AddressChooserTask? Image 3: How to Retrieve Contact Information in WP7 using the AddressChooserTask?

The first screen shows the Contact Chooser screen. When you select a contact from the list, you will get the Contact name and address as implemented above. In case you cancel and come back to the original page, it will come to the cancel case and show a message as implemented.

Hope this post was helpful to you. Stay tuned to my blog, Twitter, or Facebook to read more articles, tutorials, news, tips and tricks on various technology fields.

Reference: http://www.kunal-chowdhury.com You may like to follow me on twitter @kunal2383 or may like the Facebook page of my blog http://www.facebook.com/blog.kunal2383.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

_ Kunal Chowdhury _
Technical Lead
India India
Kunal Chowdhury is a Microsoft "Client Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik MVP, Nokia Developer Champion, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.
 
He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows 8 (WinRT). He posts his findings, articles in his technical blog and CodeProject.
 
Technical Blog: http://www.kunal-chowdhury.com
Facebook: http://facebook.com/blog.kunal
Twitter : http://twitter.com/kunal2383
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140826.1 | Last Updated 31 Mar 2012
Article Copyright 2012 by _ Kunal Chowdhury _
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid