Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Retrieve Contact Information in WP7 using the AddressChooserTask?

0.00/5 (No votes)
31 Mar 2012 1  
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:

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:

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.

// 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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here