How to Save Contact in WP7 using the SaveContactTask?


Windows Phone 7 SDK exposes API to provide user option to save any contact to the Contact list. In your app you may want to include contact saving option. This small post will help you to understand the API and after that, you will be able to use it in any of your applications.

 

The SDK has a sealed class called SaveContactTask, which you can use to implement this feature. Continue reading to know more about the class and implementation steps.

 

Know About the API

SaveContactTask is a sealed class present in the Microsoft.Phone.Tasks namespace which derives from ChooserBase of type SaveContactResult. SaveContactTask class provides a bunch of properties, which you can set to auto fill the contact informations. The class has only one method called Show() which launches the New Contact screen.

 

Here is the meta data of the SaveContactTask class, where you can see all the exposed properties that you can use:

 
namespace Microsoft.Phone.Tasks
{
    public sealed class SaveContactTask : ChooserBase<SaveContactResult>
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string MiddleName { get; set; }
        public string Nickname { get; set; }
        public string Suffix { get; set; }
        public string Company { get; set; }
        public string Title { get; set; }
        public string MobilePhone { get; set; }
        public string HomePhone { get; set; }
        public string WorkPhone { get; set; }
        public string PersonalEmail { get; set; }
        public string WorkEmail { get; set; }
        public string OtherEmail { get; set; }
        public string HomeAddressStreet { get; set; }
        public string HomeAddressCity { get; set; }
        public string HomeAddressState { get; set; }
        public string HomeAddressZipCode { get; set; }
        public string HomeAddressCountry { get; set; }
        public string WorkAddressStreet { get; set; }
        public string WorkAddressCity { get; set; }
        public string WorkAddressState { get; set; }
        public string WorkAddressZipCode { get; set; }
        public string WorkAddressCountry { get; set; }
        public string Website { get; set; }
        public string Notes { get; set; }
        public string JobTitle { get; set; }
 
        public override void Show();
    }
}

 

 

You can use the Completed event to know whether the saving operation has been successfully ended or not.

 

Implementation Steps

To implement the contact saving operation, create the instance of the class and set the optional properties that you want to auto fill when user launches the save contact screen from your application. Register for the completed event to know whether the saving operation has been successful.

 

 
// Create the instance of the SaveContactTask
var saveContactTask = new SaveContactTask();
 
// Set the optional properties that you want to auto populate with
saveContactTask.Title = "Mr.";
saveContactTask.FirstName = "Kunal";
saveContactTask.LastName = "Chowdhury";
saveContactTask.MobilePhone = "+91-8888779569";
saveContactTask.HomePhone = "(018) 741236555";
saveContactTask.WorkPhone = "(018) 1999984562";
saveContactTask.Website = "www.kunal-chowdhury.com";
 
// Register the Save Completed event
saveContactTask.Completed += SaveContactTask_Completed;
 
// Call the Show method to open the Contact Screen
saveContactTask.Show();

 

 

Finally call the Show() method to launch the New contact saving screen in your Windows Phone 7 device. Based on the SaveContactResult in the Completed event implementation, take necessary actions as demonstrated below:

 
void SaveContactTask_Completed(object sender, SaveContactResult e)
{
    switch (e.TaskResult)
    {
        case TaskResult.OK:
            // Contact Saved
            break;
 
        case TaskResult.Cancel:
            // Contact Saving Operation Cancelled
            break;
 
        default:
            break;
    }
}

 

That’s all about the coding part. When you launch it, you will see that the new phone contact screen has been auto populated with the values that you set from the code. If you want to add more details you will be able to do that from that screen as shown below:

 

Image 1: How to Save Contact in WP7 using the SaveContactTask?     Image 2: How to Save Contact in WP7 using the SaveContactTask?     Image 3: How to Save Contact in WP7 using the SaveContactTask?

 

 

Finally user needs to Save the contact by clicking the save button present in the App Bar of the screen, else the contact will not getting saved automatically. Hope this small tips was helpful for you to understand the API and the implementation process.

 

Stay tuned to my blog, twitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields.


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

0 comments

 
© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top