Introduction
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.
Steps
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.

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"
>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock
Name="txtResults"
Text="{Binding
Path=DisplayName,
Mode=OneWay}"
/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBlock
Height="41"
HorizontalAlignment="Left"
Margin="47,118,0,0"
Name="txtResults"
Text="List
of Contacts"
VerticalAlignment="Top"
Width="373"
/>
</Grid>

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)
{
try
{
lstcontacts.DataContext = e.Results;
}
catch (System.Exception)
{
txtResults.Text = "No Results Available";
}
if
(lstcontacts.Items.Any())
{
txtResults.Text = "Below is the List of Contacts";
}
else
{
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("[email protected]", 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.

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.
Conclusion
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.


sajid rajaPosted Jul 1, 2013, 11:46 PM EDT
Sree S. Please go to properties and in Manifest set required Property/ Permissions for application to run/
sree sPosted Jul 1, 2013, 10:45 PM EDT
im getting n error :An exception of type 'System.UnauthorizedAccessException' occurred in Microsoft.Phone.ni.dll but was not handled in user code
sajid rajaPosted Jun 25, 2013, 12:59 PM EDT
nice tutorial. I tried this but it is not working. I am using this with windows phone 8
Harshal JumlePosted Feb 19, 2013, 2:31 AM EST
Is the reverse of this possible ? I am trying from several days but unable to send 2,3 or 4 rows of data to server via WCF from my WP7 Check this link here is the question in detail http://stackoverflow.com/questions/14950170/send-data-to-server-from-windows-phone-7-to-web-application-using-wcf-i-am-abl
Harshal JumlePosted Feb 19, 2013, 2:31 AM EST
Is the reverse of this possible ? I am trying from several days but unable to send 2,3 or 4 rows of data to server via WCF from my WP7 Check this link here is the question in detail http://stackoverflow.com/questions/14950170/send-data-to-server-from-windows-phone-7-to-web-application-using-wcf-i-am-abl
Saurabh SharmaPosted Aug 15, 2012, 12:17 PM EDT
Hi Karthikeyan, Felt very nice after reading your post. But my problem is still not resolved after reading this post. I am facing an issue when a user wants to search for a contact number while typing the number. The search async method returns values only when complete number is passed to it whereas when we type chars it works perfectly and reflects the matching results. Can u plz help me out on this ? Regards, Saurabh Sharma [email protected]
Stephenia JacksonPosted Apr 2, 2012, 9:57 PM EDT
very interesting...keep it up...
KrishnaPosted Apr 2, 2012, 8:28 PM EDT
Hi, Really very good work, keep it up.