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

How to Retrieve a Phone Number from Contacts in WP7 using the PhoneNumberChooserTask?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
15 Apr 2012CPOL3 min read 12.7K   1   1
How can you programmatically retrieve a phone number from an existing contact list?

In the last blog post, we learnt “How to Call a Number in WP7 using the PhoneCallTask?” We also learnt, “How to Save Phone Number in WP7 using the SavePhoneNumberTask?” So, how can you programmatically retrieve a phone number from an existing contact list?

This post will help you to understand it. After reading this post, you will be able to programmatically retrieve the phone number in your application (this requires user’s interaction) using the PhoneNumberChooserTask. Don’t forget to read the complete post as this will help you to know about the internal implementation of the SDK API too.

Know About the API

PhoneNumberChooserTask” is a sealed class present in the “Microsoft.Phone.Tasks” namespace. It inherits “ChooseBase” of type “PhoneNumberResult” which returns the selected phone number and display name. PhoneNumberChooserTask has a method called Show() which brings the contact chooser UI in the screen.

Here is the meta data of the PhoneNumberChooserTask:

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

Today I decompiled the SDK library for PhoneNumberChooserTask class and learnt a lot about the actual implementation of the API. You will find it really useful.

Here is the actual API implementation of the class:

C#
namespace Microsoft.Phone.Tasks
{
  public sealed class PhoneNumberChooserTask : ChooserBase<PhoneNumberResult>
  {
    private const int StaticFilterHasPhone = 4;
    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 = 4;
      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;
          PhoneNumberResult e = new PhoneNumberResult(TaskResult.OK);
          e.PhoneNumber = 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 PhoneNumberResult(TaskResult.Cancel), 

                                                                        fireThisHandlerOnly);

    }
  }
}

I hope that the above decompiled version of the class was very helpful for you to understand the actual implementation.

Implementation Steps

Let’s implement a sample code and discuss how it works using a simple application. First of all, you have to create an instance of the class PhoneNumberChooserTask and register for the completed event before calling the Show() method. Here is the code snippet for your reference:

C#
var phoneNumberChooserTask = new PhoneNumberChooserTask();
phoneNumberChooserTask.Completed += PhoneNumberChooserTaskCompleted;
phoneNumberChooserTask.Show();

In the completed event implementation, extract the PhoneNumberResult and retrieve the DisplayName and PhoneNumber from it. You can store it or pass it to PhoneCallTask to place a call like the below code snippet:

C#
void PhoneNumberChooserTaskCompleted(object sender, PhoneNumberResult e)
{
    var phoneCallTask = new PhoneCallTask 
                            { 
                                DisplayName = e.DisplayName, 
                                PhoneNumber = e.PhoneNumber 
                            };
    phoneCallTask.Show();
}

Not only that, you can do any operation that your application wants after retrieving this information. Let’s see how it works in the Windows Phone 7 device.

Once you call the Show() method, the first screen will appear in the device asking user to chose the contact and in the completed event once you get the information, it will ask you to place a call (as per our implementation) and then the 3rd screen shows the call to that contact as below:

Screenshot 1 : How to Retrieve Phone Number from Contacts in WP7 using the PhoneNumberChooserTask?     Screenshot 2 : How to Retrieve Phone Number from Contacts in WP7 using the PhoneNumberChooserTask?     Screenshot 3 : How to Retrieve Phone Number from Contacts in WP7 using the PhoneNumberChooserTask?

I hope that, this post was helpful to you to understand the basic and implementation of the API. If you like these posts, don’t forget to share with your friends, colleagues or others whom you believe that this post will be useful. Don’t forget to share your feedback as it will help me to improve my future posts and also encourage me to deliver more for the benefit of you.

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 newsletter to your registered email address. We will not share your email address to 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.

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

 
SuggestionFormatting of the code blocks Pin
Wendelius14-Apr-12 9:00
mentorWendelius14-Apr-12 9:00 

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.