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

How to Retrieve Email Address in WP7 using the EmailAddressChooserTask?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
21 Apr 2012CPOL2 min read 11.9K   1   1
How to retrieve an email address from a contact list with a simple example (including the internal implementation of the API).

In the last two blog posts, we learnt how to save email address in WP7 and how to send email. We also learnt the internal implementation of the APIs used by the Windows Phone 7 SDK.

Today in this blog post, we will explore how to retrieve email address from the contact list with a simple example (including the internal implementation of the API). Don’t forget to provide your feedback. If you have any queries, let me know. I will try to answer you as soon as I can.

Related Posts

Know About the API

EmailAddressChooserTask is a sealed class present in the Microsoft.Phone.Tasks namespace and derives from ChooserBase of type EmailResult. It contains only a single method named Show() when called opens the Email Chooser UI in the screen.

It allows an application to launch the Contacts application. Use this to obtain the email address of a contact selected by the user.

Here is the meta data of the EmailAddressChooserTask class:

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

If we explore the decompiled version of the class, we will see the actual implementation of the SDK API. Here is the original implementation of the class:

C#
namespace Microsoft.Phone.Tasks
{
  public sealed class EmailAddressChooserTask : ChooserBase<EmailResult>
  {
    private const int StaticFilterHasEmail = 8;
    private const string AppUri = 
            "app://5B04B775-356B-4AA0-AAF8-6491FFEA5615/ChoosePropertyOfExistingPerson";

    public override void Show()
    {
      if (!ChooserHelper.NavigationInProgressGuard((Action) (() => this.Show())))
        return;
      byte[] buffer = ChooserHelper.Serialize(this.BuildParameterPropertyBag());
      Uri appUri = new Uri(this.BuildUri(), UriKind.Absolute);
      base.Show();
      ChooserHelper.Invoke(appUri, buffer, (IChooser) this._genericChooser);
    }

    internal override string BuildUri()
    {
      return "app://5B04B775-356B-4AA0-AAF8-6491FFEA5615/ChoosePropertyOfExistingPerson";
    }

    internal override ParameterPropertyBag BuildParameterPropertyBag()
    {
      ParameterPropertyBag parameterPropertyBag = new ParameterPropertyBag();
      parameterPropertyBag.CreateProperty("RestrictionType").Int32Value = 8;
      return parameterPropertyBag;
    }

    internal override void OnInvokeReturned(byte[] outputBuffer, Delegate fireThisHandlerOnly)
    {
      bool flag = false;
      if (outputBuffer.Length > 0)
      {
        ParameterPropertyBag parameterPropertyBag = new ParameterPropertyBag(outputBuffer, 
                                                                 (uint) outputBuffer.Length);
        ParameterProperty property1 = parameterPropertyBag.GetProperty("PickerPropertyValue");
        if (property1.ValueType == ParameterPropertyValueType.ValueType_String && 
                                             !string.IsNullOrEmpty(property1.StringValue))
        {
          flag = true;
          EmailResult e = new EmailResult(TaskResult.OK);
          e.Email = property1.StringValue;
          ParameterProperty property2 = parameterPropertyBag.GetProperty("OutDisplayName");
          if (property2.ValueType == ParameterPropertyValueType.ValueType_String)
            e.DisplayName = property2.StringValue;
          this.FireCompleted((object) this, e, fireThisHandlerOnly);
        }
      }
      if (flag)
        return;
      this.FireCompleted((object) this, new EmailResult(TaskResult.Cancel), 
                                                        fireThisHandlerOnly);
    }
  }
}

Once you go through the decompiled version of the class from the above code snippet, you will understand the code implementation easily.

Implementation Steps

To integrate this in your application, first of all you need to create the instance of the EmailAddressChooserTask and register for the Completed event (if you want to handle the task result). Call the Show() method to launch the chooser screen.

Here is the code snippet:

C#
var emailAddressChooserTask = new EmailAddressChooserTask();
emailAddressChooserTask.Completed += EmailAddressChooserTaskCompleted;
emailAddressChooserTask.Show();

Here is the completed event, where you can handle the result easily. The “EmailResult” returns the contact display name and email address. Based on the TaskResult, take care of user interaction.

C#
void EmailAddressChooserTaskCompleted(object sender, EmailResult e)
{
    switch (e.TaskResult)
    {
        case TaskResult.OK:
            // On Success
            MessageBox.Show(e.DisplayName + ": " + e.Email);
            break;

        case TaskResult.Cancel:
            // Interrupted by the user

            break;

        default:
            break;
    }
}

Here is the screenshot of what you will actually see once you run the above code. When you call the Show() method, this will popup the contact list. When you click on the contact, it will come to the completed event and as per our implementation, it will show the name and email address of the specified contact.

Screenshot 1: How to Retrieve Email Address in WP7 using the EmailAddressChooserTask? Screenshot 2: How to Retrieve Email Address in WP7 using the EmailAddressChooserTask?

I hope that this post was very useful for you to understand the SDK API, its internal code implementation and the sample code implementation. Please leave your feedback below.

Stay tuned to my blog, Twitter, or Facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletters to your registered email address. We will not share your email address with anybody as we respect privacy.

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)


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

 
QuestionWP7 Pin
Manov rao24-Apr-12 19:54
Manov rao24-Apr-12 19:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.