In this article I will demonstrate how to use Contacts class to get contact details in Windows Phone. I will use Contacts and Contact class of Microsoft.Phone.UserData.
Let's write code.
Step 1: Create a button in the MainPage.xaml to get Contacts details.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <Button x:Name="btnContacts" Margin="20,100,0,0" Height="80" Click="btnContacts_Click" Content="Get Contacts"></Button> </Grid>
Step 2: Add Microsoft.Phone.UserData
using Microsoft.Phone.UserData;
We will use Contacts and Contact class of Microsoft.Phone.UserData. Contacts class allows user to get Contacts with the specified search criteria.
Contacts initializes the Contacts class.
SearchAsync asynchronously searches for contacts.
Accounts get the accounts from which contacts are available.
SearchCompleted event triggers when search for contacts completes.
Contact class contains the data of particular contact.

GetPicture gets picture of contact
Addresses is IEnumerable which retuns addresses associated with contact as ContactAddress
BirthDays is IEnumerable which retuns bithdays associated with contact as DateTime
Children is IEnumerable which returns children associated with contact as String
Companies is IEnumerable which returns companies associated with contact as ContactCompanyInformation
DisplayName gets the display name of the contact
EmailAddresses is IEnumerable which returns emails associated with contact as ContactEmailAddress
IsPinnedStart indicates whether contact is pinned to start or not.
Notes is IEnumerable which returns notes associated with contact as String
PhoneNumbers is IEnumerable which returns phone numbers associated with contact as ContactPhoneNumber
SignificantOthers is IEnumerable which returns significant others associated with contact as String
Websites is IEnumerable which returns websites associated with contact as String
Step 3: Add btnContacts_Click method in MainPage.xaml.cs. SearchAsync take filter string, filterkind to search in Contacts list.
FilteKind can be DisplayName, EmailAddress, PhoneNumber, PinnedToStart or none.
private void btnContacts_Click(object sender, RoutedEventArgs e) { Contacts contacts = new Contacts(); contacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);
//Start the asynchronous search. contacts.SearchAsync("L", FilterKind.DisplayName, null); }
Step 4: Add reference of System.Device. It will used to details of Contact Addresses.
Step 5: Now place the Contacts_SearchCompleted method which will trigger when search of Contact from Contacts is completed.
void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e) { foreach (Contact contact in e.Results) { MessageBox.Show(contact.DisplayName); foreach(ContactPhoneNumber contactPhNumber in contact.PhoneNumbers) { MessageBox.Show(contactPhNumber.Kind.ToString() + " is " + contactPhNumber.PhoneNumber); }
foreach (ContactEmailAddress contactEmail in contact.EmailAddresses) { MessageBox.Show(contactEmail.Kind.ToString() + " is " + contactEmail.EmailAddress); }
foreach (string website in contact.Websites) { MessageBox.Show(website); }
foreach (string children in contact.Children) { MessageBox.Show(children); }
foreach (DateTime birthDay in contact.Birthdays) { MessageBox.Show(birthDay.ToShortDateString()); }
foreach (ContactCompanyInformation contactCompInfo in contact.Companies) { MessageBox.Show("Company" + contactCompInfo.CompanyName); MessageBox.Show("Job Title" + contactCompInfo.JobTitle); MessageBox.Show("Office Location" + contactCompInfo.OfficeLocation); }
foreach (string significantOthers in contact.SignificantOthers) { MessageBox.Show(significantOthers); }
foreach (ContactAddress contactAddress in contact.Addresses) { System.Device.Location.CivicAddress address = contactAddress.PhysicalAddress; MessageBox.Show("Address Line 1" + address.AddressLine1); MessageBox.Show("Address Line 2" + address.AddressLine2); MessageBox.Show("Floor" + address.FloorLevel); MessageBox.Show("Building" + address.Building); MessageBox.Show("City" + address.City); MessageBox.Show("PostalCode" + address.PostalCode); MessageBox.Show("State Province" + address.StateProvince); MessageBox.Show(address.CountryRegion); }
foreach (string notes in contact.Notes) { MessageBox.Show(notes); } } }
This ends the aticle of getting Contact information in Windows Phone.
|