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

How to Save Contact in WP7 using the SaveContactTask?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
28 Mar 2012CPOL2 min read 8.8K   1  
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. The SaveContactTask class provides a bunch of properties, which you can set to auto fill the contact information. 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:

C#
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.

C#
// 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:

C#
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, the 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 tip was helpful for you to understand the API and the implementation process.

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

 
-- There are no messages in this forum --