How do I grab the contact list of a windows 7 phone for use inside a win7 phone app?
With the earlier version of the Windows Phone 7 SDK, it was only possible to retrieve the phone number or email address and a few more with the Choosers. Now, with the 7.1 Mango SDK, it is possible to retrieve more information from the contact, like Address
, DisplayName
, EmailAddresses
etc.
I will show you how to retrieve all contacts from Windows Phone 7 using C#.
The Contacts
Class is defined in the namespace Microsoft.Phone.UserData
and extends from PhoneDataSharingContext
and provides a few methods and events for interacting with a user’s contact data.
public MainPage()
{
InitializeComponent();
Contacts objContacts = new Contacts();
objContacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(objContacts_SearchCompleted);
objContacts.SearchAsync(string.Empty, FilterKind.None, null);
}
void objContacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
foreach (var result in e.Results)
{
lst.Add("Name : " + result.DisplayName + " ; Phone Number : " + result.PhoneNumbers.FirstOrDefault());
}
}
Contacts
can also enable the user to search for the contact with the SearchAsync
method. The FilterKind
determines the field that will be used for filtering like PhoneNumber
, DisplayName
or EmailAddress
etc. When it is None
, it can list all the contacts.
Note that I have used the emulator since I don't have the device with Mango currently.
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
Contacts objContacts = new Contacts();
objContacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(objContacts_SearchCompleted);
objContacts.SearchAsync(string.Empty, FilterKind.None, null);
}
void objContacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
var ContactsData = from m in e.Results
select new MyContacts
{
DisplayName = m.DisplayName,
PhoneNumber = m.PhoneNumbers.FirstOrDefault()
};
var MyContactsLst = from contact in ContactsData
group contact by contact.DisplayName into c
orderby c.Key
select new Group<MyContacts>(c.Key, c);
longlist1.ItemsSource = ContactsData;
}
}
public class MyContacts
{
public string DisplayName { get; set; }
public ContactPhoneNumber PhoneNumber { get; set; }
}
You can have the user select a contact from their contact list one at a time by using PhoneNumberChooserTask (example, SDK 7.0):
http://msdn.microsoft.com/en-us/library/ff769543(v=vs.92).aspx#BKMK_Phone
Also, in the Beta of the new Windows Phone SDK 7.1 release (aka Mango) one can now access all the phone's contacts:
http://www.ginktage.com/2011/05/how-to-retreive-all-contacts-from-windows-phone-7-using-c/
Note: I heard that MS will not allow apps developed in Mango to be released into the app store until fall 2011.
It's doable in Windows Phone OS 7.1
Here's the MSDN article on How To Access Contact List Data For Windows Phone
And here's a code snippet from the article:
using Microsoft.Phone.UserData;
private void ButtonContacts_Click(object sender, RoutedEventArgs e)
{
Contacts cons = new Contacts();
//Identify the method that runs after the asynchronous search completes.
cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);
//Start the asynchronous search.
cons.SearchAsync(String.Empty, FilterKind.None, "Contacts Test #1");
}
void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
//Do something with the results.
MessageBox.Show(e.Results.Count().ToString());
}
Unfortunately, the contacts API is not exposed in the current version of the API. The closest you can get is using something from the Tasks namespace:
http://msdn.microsoft.com/en-us/library/ff428753(v=VS.92).aspx
The two you can look for are the PhoneNumberChooseTask and EmailAddressChooserTask.
alternatively, since all WP7 phones use the live contacts to sync over the air, you could always just investigate doing a web request and accessing the Windows Live Contacts API. I understand this is probably not what you're looking for, but it's probably the best option right now.