Click here to Skip to main content
15,884,388 members
Articles / Mobile Apps / Windows Phone 7

How to Retrieve Contact Information in WP7 using the AddressChooserTask?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
31 Mar 2012CPOL2 min read 21.1K   2  
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 want 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 the user will be able to select the desired contact.

Here is the meta data of the AddressChooserTask:

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

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:

C#
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 much 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.

C#
// 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:

C#
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.

This article was originally posted at http://www.kunal-chowdhury.com/feeds/posts/default

License

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


Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead 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 app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
-- There are no messages in this forum --