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 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 call Show(), which brings the contact chooser UI in the screen.

 

Here is the meta data of the PhoneNumberChooserTask:

 
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:

 

 
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:

 
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:

 

 
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.

Have a question? Or, a comment? Let's Discuss it below...

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.