Introduction
The tip describes how to select all the contacts and respective data in Windows Phone 7 and 8.
Background
Windows phone API allows us to access the phone book contact list using
the code. Contacts are fetched in an observable list with details like Display Name, Company, Phone Number, etc. bundled in the Contacts
object. These contacts can then be displayed in a list box with the required details.
Following examples demonstrate the same.
Using the Code
We will use the
Contacts
class to fetch the details of phone book contacts.
To start with Windows Phone
Contacts
class, the first and foremost requirement is that of adding a capability of
ID_CAP_CONTACTS
in the
WMAppManifest.xml file.
To add a capability, either edit the XML or select the Capabilities tab from
WMAppManifest.xml file and select the
ID_CAP_CONTACTS
check box located in the left part of the screen.
XML representation of the capability in WMAppManifest.xml:
<Capabilities>
<Capability Name="ID_CAP_CONTACTS" />
</Capabilities>
Pictorial representation of the capabilities in WMAppManifest.xml:
Now it’s time to move with the code to fetch the contact details.
Let’s display all contacts using the search method from the Contacts
class in MVVM pattern.
MainPage.xaml
<Grid x:Name="LayoutRoot" Background="Transparent">
<ListBox x:Name="lbMyContactNames" ItemsSource="{Binding Path=ListContactData}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=ContactName}" Margin="20" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
MainPage.xaml.cs
public MainPage()
{
InitializeComponent();
lbMyContactNames.DataContext = new ContactsDemoViewModel();
}
ContactsDemoViewModel.cs
using Microsoft.Phone.UserData;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace ContactsChooser
{
class ContactsDemoViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private Contacts myPersonalContacts;
private List<string> listData;
public List<ContactsDemoModel> ListContactData{get;set;}
public ContactsDemoViewModel() {
myPersonalContacts = new Contacts();
myPersonalContacts.SearchCompleted += myPersonalContacts_SearchCompleted;
listData = new List<string>();
string myName = "Anobik Dey";
myPersonalContacts.SearchAsync(string.Empty, FilterKind.DisplayName, myName);
}
void myPersonalContacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
ListContactData = new List<ContactsDemoModel>();
for (int i = 0; i < e.Results.Count(); i++) {
ListContactData.Add(new ContactsDemoModel()
{ContactName = e.Results.ElementAt(i).DisplayName});
}
RaisePropertyChanged("ListContactData");
}
protected void RaisePropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
}
ContactsDemoModel.cs
class ContactsDemoModel
{
public string ContactName{get;set;}
}
Points of Interest
Using the above code, we can display the names of all contacts in the
list box. For selecting a particular set of contacts, we can use the SearchAsync
function with the search parameters while for fetching all the contacts. we can search with a string.Empty
or a blank (""
) argument as a result of which all the contacts will be fetched.