How to Save Phone Number in WP7 using the SavePhoneNumberTask?


Do you want to integrate a task in your Windows Phone 7 application that will help users to save their contact details e.g. a phone number in their Contact List? If you want to do this, there is a class called SavePhoneNumberTask which will allow you to implement that task.

 

This small post will help you to understand the basics about the API and details about the implementation stuff with step-by-step details.

 

Know About the API

The Windows Phone 7 SDK provides a sealed class called SavePhoneNumberTask present in the Microsoft.Phone.Tasks namespace that provides you API to save a Phone number in the users contact list programmatically. The class provides a property called PhoneNumber of type string. You can programmatically assign that value to auto populate the screen where the saving operation will happen. The Show() method initiates the task by opening the proper screen in the UI.

 

Here is the meta data of the class:

 

 
namespace Microsoft.Phone.Tasks
{
    public sealed class SavePhoneNumberTask : ChooserBase<TaskEventArgs>
    {
        public string PhoneNumber { get; set; }
        public override void Show();
    }
}

 

 

Here is a list of related posts that you might want to read:

  1. How to Save Contact in WP7 using the SaveContactTask?
  2. How to Retrieve Contact Information in WP7 using the AddressChooserTask?
  3. How to Search for a Contact in WP7 using the Contacts class?

 

Implementation Steps

If you want to implement the task in your WP7 application, create the instance of the SavePhoneNumberTask. If you want to auto populate the value of Phone number, just set the PhoneNumber property from the code and this will show up in the interface. If you don’t do this, the user will have to set it manually.

 

Register the Completed event of the SavePhoneNumberTask to check whether the user successfully saved the number to the contact list or just cancelled it and then call the Show() method at the end.

 

Here is the code snippet of the same:

 
var savePhoneNumberTask = new SavePhoneNumberTask { PhoneNumber = "9878552581" };
savePhoneNumberTask.Completed += SavePhoneNumberTaskCompleted;
savePhoneNumberTask.Show();

 

 

In the completed event, check the TaskEventArgs where you will find TaskResult. If the result is “OK”, that means that the user successfully saved the number in his contact list. If it comes as “Cancel”, that means that the saving operation interrupted by the user.

 

Here is the completed event implementation for our demo:

 

 
void SavePhoneNumberTaskCompleted(object sender, TaskEventArgs e)
{
    switch (e.TaskResult)
    {
        case TaskResult.OK:
            // Phone number saved successfully by the user
            MessageBox.Show("Phone Number saved successfully");
            break;
 
        case TaskResult.Cancel:
            // Saving interrupted by the user
            MessageBox.Show("Phone Number saving interrupted by the user");
            break;
 
        default:
            break;
    }
}

 

That’s all about the implementation steps for our demo application. If you want to do any specific operations, you can do that in that completed event implementation based on the TaskResult.

 

Demo

When you run the above code by calling the Show() method, it will show you the below UI in the screen where you can add the phone number to an existing contact or create a new contact with the phone number provided:

 

Screenshot 1 : How to Save Phone Number in WP7 using the SavePhoneNumberTask?

 

Either you click any existing contact or create a new contact, this will show you the below screen where you will be able to modify the number or set it to specific type of phone number:

 

Screenshot 2 : How to Save Phone Number in WP7 using the SavePhoneNumberTask?

 

The next screen will allow you to modify the contact information for that particular contact by pressing the appropriate link as shown here:

 

Screenshot 3 : How to Save Phone Number in WP7 using the SavePhoneNumberTask?     Screenshot 4 : How to Save Phone Number in WP7 using the SavePhoneNumberTask?

 

Now do you want to call to that number? Yes, you can do that too from the screen. Tap the number to begin calling. Here are the screenshots of it:

 

Screenshot 5 : How to Save Phone Number in WP7 using the SavePhoneNumberTask?     Screenshot 6 : How to Save Phone Number in WP7 using the SavePhoneNumberTask?

 

 

If you complete the contact saving operation successfully or interrupt the task, you will get proper message as shown below (as per our implementation logic):

 

Screenshot 7 : How to Save Phone Number in WP7 using the SavePhoneNumberTask?     Screenshot 8 : How to Save Phone Number in WP7 using the SavePhoneNumberTask?

 

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.


8 comments

  1. Will these new add number going to add in SQL Database

    ReplyDelete
  2. Hi Debal, this saves in the Phone memory itself.

    ReplyDelete
  3. How can I add contact details in SQL database 2008 ?

    ReplyDelete
  4. Hi Debal,
    If you want to store any data in external server, you have to create web service (e.g. WCF service) and call that service from your application. In your service you will be able to store to the SQL Server DB.

    ReplyDelete
  5. Can I Use .dbml in window phone WCF services , I mean Linq to Sql database connection

    ReplyDelete
  6. Debal, you don't have to worry about it. You can use anything in the server side as long as you have a service to communicate from your WP7 app.

    ReplyDelete
  7. Hello Kunal sir,
    i am manish,i want to save all contacts directly to the windows phone 7 or 7.8... from the server.
    what should i do.. ??

    ReplyDelete
    Replies
    1. Hi Manish,

      Due to security reason, the API is limited to do a silent setup. It will need user intervention for each contact to save in your phone device.

      Delete


 
© 2008-2014 Kunal-Chowdhury.com | Designed by Kunal Chowdhury
Back to top