Click here to Skip to main content
11,490,408 members (41,605 online)
Click here to Skip to main content

Tagged as

How to Save Contact in WP7 using the SaveContactTask?

, 28 Mar 2012 CPOL 4.4K 1
Rate this:
Please Sign up or sign in to vote.
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.

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:

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();
    }
}</savecontactresult>

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kunal Chowdhury (@kunal2383)
Team Leader
India India
Kunal Chowdhury is a Microsoft "Windows Platform Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik MVP, Nokia Developer Champion, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer 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 Store (WinRT) app development. He posts his findings, articles, tutorials in his technical blog and CodeProject.

Technical Blog: http://www.kunal-chowdhury.com
Facebook: http://facebook.com/blog.kunal
Twitter : http://twitter.com/kunal2383
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 28 Mar 2012
Article Copyright 2012 by Kunal Chowdhury (@kunal2383)
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid