Over a million developers have joined DZone.

Windows Phone 7 Development - Working with Contacts

DZone's Guide to

Windows Phone 7 Development - Working with Contacts

· Mobile Zone ·
Free Resource

In this article we are going to see how to work with inbuilt contacts data and use it across our requirement to manipulate the data. Windows Phone 7.1 provides a read only access to the data available locally across the device. We can query the data and select the users based on the search filters and also we can do multiple manipulations in order to perform some operation with the user contact information’s.

Let us see the step by step process on how to use the contacts for querying the data. To launch the contacts we need to use the AddressChooserTask which is used to select the contacts. The queried result can be collected in the AddressResult object and can be used to list it as per the requirement. Let us start with creating a new application and start using the AddressChooserTask.


Open Visual Studio 2010 and create a new Silverlight for Windows Phone 7 project and provide a valid project name as shown in the screen below.

2012-01-17 21h06_35

Now we will add a button which triggers an event to query the data and get the contacts in a list. Add the below XAML code, or we can directly drag and drop the controls as shown in the screen below.

XAML Code:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Button Content="Get Contacts" Height="83" HorizontalAlignment="Left" Margin="30,16,0,0" Name="btnContacts" VerticalAlignment="Top" Width="402" Click="btnContacts_Click" />
<ListBox Name="lstcontacts" ItemsSource="{Binding}" Margin="47,188,36,52" >
<TextBlock Name="txtResults" Text="{Binding Path=DisplayName, Mode=OneWay}" />
<TextBlock Height="41" HorizontalAlignment="Left" Margin="47,118,0,0" Name="txtResults" Text="List of Contacts" VerticalAlignment="Top" Width="373" />

2012-01-17 21h27_04

Now we will add some code to the code behind page for querying the data from the contacts database. To do that go to the button click event and write the code below. Basically the Windows Phone 7 Emulator has some default contacts which can be tested by pulling to the list in this article. We need to add the namespace in order to access the data.

using Microsoft.Phone.UserData;

Now in the button click event add the below code. From the code we can see the Contact class been used to query the details. Here we will be using the SearchAsyc method to do the search with the object and provide the result set to the list as shown in the code below.

C# Code:

private void btnContacts_Click(object sender, RoutedEventArgs e)
Contacts cContacts = new Contacts();
cContacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(ContactsSearch);
cContacts.SearchAsync(String.Empty, FilterKind.DisplayName, null);
void ContactsSearch(object sender, ContactsSearchEventArgs e)
lstcontacts.DataContext = e.Results;
catch (System.Exception)
txtResults.Text = "No Results Available";

if (lstcontacts.Items.Any())
txtResults.Text = "Below is the List of Contacts";
txtResults.Text = "No Results Available";

In the above code if we see we are querying the contacts class object and bind it to the list box using the data context and do some small user information with the message. In the SearchAsync method we provide different filters that can be used to access the data. Below is the list of filters that can be provided based on the requirement.

  • Search All Contacts – SearchAsync(String.Empty, FilterKind.None, null)
  • Get Pinned Contacts – SearchAsync(String.Empty, FilterKind.PinnedToStart, null)
  • Search by Display Name – SearchAsync(“Karthik”, FilterKind.DisplayName, null)
  • Search by Email ID – SearchAsync(“Karthik@f5debug.net”, FilterKind.EmailAddress, null)
  • Search by Phone No – SearchAsync(“123-456-7890″, FilterKind.PhoneNumber, null)

Now we are done with the application just to build and execute the application to check in the emulator press F5 or click on Build and execute the solution. We can see the end result by pressing the button once the application is loaded as shown in the screen below.

2012-01-17 21h36_19

The list of contacts which are shown in the above screen is the default contacts available with the Emulator for testing purposes. We can use these contacts to do the testing and add or delete it based on the requirement.


So in this article we have seen how to use the contacts data search using the AddressChoosertask and list the data to the list view.

Thanks for reading my article. If you like my blog and if you are interested in getting the latest updates on new articles, kindly follow me through one of these options.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}