October 25, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Building a Custom Contact Store in Your Windows Phone 8 Application

  • June 28, 2013
  • By Vipul Patel
  • Send Email »
  • More Articles »

Introduction

With the release of Windows Phone 8, Microsoft added support for Windows Phone 8 applications to create and maintain their own custom contact stores. These contact stores can be accessible through the built-in contacts experience.  Moreover, programmatic access to maintain the contacts (create/update/delete) was also provided.

Contact Store Fundamentals

Windows Phone 8 allows applications built on the platform to have their own custom contacts. Applications can create their own custom contact store, where contacts can be added with the standard properties like name and address. In addition, custom properties can also be added to these contacts – properties that are application specific.

While application specific properties are not visible in the People hub, standard contact properties like name and address, are accessible in the People hub.

Each application has one and only one custom contact store, which can be opened by calling the ContactStore.CreateOrOpenAsync() API. A custom contact store by default, has read access at system-level and limited read permissions for other applications.

A contact is created by calling the StoredContact constructor. Each StoredContact instance has an operating system assigned value for the localId field, and it also has a field, remoteID,  for storing the contact identifier in a remote store.

Additional contact properties can be added/updated by calling GetExtendedPropertiesAsync, an API which returns a dictionary of properties that can be updated.

Hands On

Create a new Visual Studio 2012 project titled WindowsPhoneCustomContactsDemo.

A New Visual Studio 2012 Project
A New Visual Studio 2012 Project

When prompted, selected the Windows Phone OS 8.0 as the target Windows Phone OS version and click OK (it should be the default).

New Windows Phone Application
New Windows Phone Application

Open Windows Phone Application manifest file by double-clicking on the WMAppManifest.xml file in Solution Explorer.

Windows Phone Application Manifest File
Windows Phone Application Manifest File

Ensure that the ID_CAP_CONTACTS is selected to provide the application with the capability to access the contacts.


Ensure that the ID_CAP_CONTACTS is Selected
Ensure that the ID_CAP_CONTACTS is Selected

Next, add a couple of textboxes, textblocks and buttons as shown below.

Add Textboxes, Textblocks and Buttons
Add Textboxes, Textblocks and Buttons

FirstName and LastName are standard contact properties, which are visible in the People Hub. FooBar is an application-specific property and that property will not be available in the People hub.

Next,add the following using statement to include the necessary namespace for accessing contacts.

//MainPage.xaml.cs

using
Windows.Phone.PersonalInformation;

Next, add a global variable for the contactStore.

public partial class MainPage : PhoneApplicationPage
    {
        ContactStore contactStore;
        // Constructor
        public MainPage()
 

We then create a method to initialize the contact store.

private async void CreateContactStore()
        {
            contactStore = await ContactStore.CreateOrOpenAsync();
        }

We will call this method from the constructor of MainPage.

public MainPage()
        {
            InitializeComponent();
            CreateContactStore();
            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

Next, we implement the event handler for the click event of the AddContactButton.

private async void buttonAddContact_Click(object sender, RoutedEventArgs e)
        {
            
            StoredContact newContact = new StoredContact(contactStore);
            newContact.GivenName = textBoxFirstName.Text;
            newContact.FamilyName = textBoxLastName.Text;
            IDictionary<string, object> extprops = await newContact.GetExtendedPropertiesAsync();
            extprops.Add("foobar", textBoxFooBar.Text);
            await newContact.SaveAsync();
            MessageBox.Show("Contact Saved");
            textBoxFirstName.Text = "";
            textBoxLastName.Text = "";
            textBoxFooBar.Text = "";
 
        }
 

Next, we implement the list all custom contacts. We will leverage the empty search query to list all the contacts.

On the click event of SearchContacts, add the following code.

private async void buttonSearchContacts_Click(object sender, RoutedEventArgs e)
        {
            if(contactStore == null) 
                CreateContactStore();
            ContactQueryResult result = contactStore.CreateContactQuery();
            IReadOnlyList<StoredContact> contactList = await result.GetContactsAsync();
            if (contactList.Count > 0)
                longListContacts.ItemsSource = (IList)contactList;
            else
                MessageBox.Show("No contacts returned");
        }
 

Compile the application. If you have any trouble compiling the error, you can download the source code of the application.

Now run the application and check out the application. The FooBar property will not be visible from the People hub, the standard properties will be available.

Summary

In this article, we learned about custom contact store. I hope you have found this information useful.

About the author

Vipul Patel is a Program Manager currently working at Amazon Corporation. He has formerly worked at Microsoft in the Lync team and in the .NET team (in the Base Class libraries and the Debugging and Profiling team). He can be reached at vipul_d_patel@hotmail.com


Tags: Windows Phone 8, Visual Studio 2012




Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel