Floating FB popout byF5debug

Learn Windows Phone 7 Development in 31 Days – Day 6 – Working with Contacts in WP7

 

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.    [more]

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.

clip_image002

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:

01

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

02

<Button Content="Get Contacts" Height="83" HorizontalAlignment="Left" Margin="30,16,0,0" Name="btnContacts" VerticalAlignment="Top" Width="402" Click="btnContacts_Click" />

03

<ListBox Name="lstcontacts" ItemsSource="{Binding}" Margin="47,188,36,52" >

04

<ListBox.ItemTemplate>

05

<DataTemplate>

06

<TextBlock Name="txtResults" Text="{Binding Path=DisplayName, Mode=OneWay}" />

07

</DataTemplate>

08

</ListBox.ItemTemplate>

09

</ListBox>

10

<TextBlock Height="41" HorizontalAlignment="Left" Margin="47,118,0,0" Name="txtResults" Text="List of Contacts" VerticalAlignment="Top" Width="373" />

11

</Grid>

clip_image004

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.

1

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:

01

private void btnContacts_Click(object sender, RoutedEventArgs e)

02

{

03

Contacts cContacts = new Contacts();

04

cContacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(ContactsSearch);

05

cContacts.SearchAsync(String.Empty, FilterKind.DisplayName, null);

06

}

07

void ContactsSearch(object sender, ContactsSearchEventArgs e)

08

{

09

try

10

{

11

lstcontacts.DataContext = e.Results;

12

}

13

catch (System.Exception)

14

{

15

txtResults.Text = "No Results Available";

16

}

17

 

18

if (lstcontacts.Items.Any())

19

{

20

txtResults.Text = "Below is the List of Contacts";

21

}

22

else

23

{

24

txtResults.Text = "No Results Available";

25

}

26

}

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.

clip_image006

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.

About Author: Karthikeyan Anbarasan, Microsoft MVP (Most Valuable Professional) in ASP.NET/IIS Architecture. He is the Founder and Chief Editor of the www.f5debug.net website and has authored books on Windows Phone and Business Intelligence(SSIS). He is also a Passionate Speaker and a Blogger on Microsoft Technologies.

You can Join Me On: Facebook, Twitter, Google Plus, LinkedIn

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Recent Posts

Random Posts

Most Viewed

URL Shortner

Create your own short urls!!!

Site Counter

free counters

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer. For accuracy and official reference refer to MSDN/ TechNet. I have documented my personal experience on this blog.

Protected by Copyscape Web Plagiarism Finder

Site Meter