How to Retreive all contacts from Windows Phone 7 using C# ?

With the earlier version of the Windows phone 7 SDK , it was only possible to retreive the phone number or email address and few more with the Choosers .

Now with the 7.1 Mango SDK , it is possible to retreive more information from the contact like Address , DisplayName,EmailAddresses  etc…

In this article , i show you how to How to retreive 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 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 .

Here’s a little formatted display of the Contacts and their Phone Number . Note that i have used the emulator since i dont 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; }

}

    11 Comments

  1. Manjunath
    June 1, 2011
    Reply

    Hi,

    I installed mango sdk but now i am not able to see the option to add a contact in emulator do we have to unlock it or do something else?

    Also Visual studio 2010 hangs and has been slow since i installed new Mango SDK update…

  2. matro
    June 17, 2011
    Reply

    hello there,

    I have the same question of Manjunath. additionally is it possible to add appointments?

  3. June 18, 2011
    Reply

    Hi Matro ..

    The below link / discussion should help ..

  4. June 18, 2011
    Reply

    Did u earlier have the option to add contacts via Emulator ??

  5. Dare
    June 2, 2011
    Reply

    Ok I am trying to get all contacts with the second example but my application throws an exception on the select new Group(c.Key, c);
    I do not knw what is that Group my application can not recognize that group.

  6. June 2, 2011
    Reply

    @Dare … Groups is simple class that extents IEnumerable…
    I needed this because of the the control that i used it from Silverlight toolkit .

  7. Dare
    June 2, 2011
    Reply

    @Senthil Kumar. Thank u for the fast reply. I solve my problem and I succeeded to get all contacts and through a wcf service to save them in a database. Now I am getting problems when I transfer the contacts from the database to my phoneApplication. I am doing that successfully but except the phoneNumber I can not save nothing else on the phone. Do u now why and can u help me please. Thanks in advance.

  8. June 18, 2011
    Reply

    To save Phone Number u need to use the SavePhoneNumber Task …

  9. developer
    October 31, 2011
    Reply

    It is weird that window phone forbids application to fetch the user’s Phone Number, while allowing to fetch user’s contacts’ phone numbers.

  10. malik umar hassan
    December 5, 2011
    Reply

    please tell me how to bind contact picture with list box

  11. Sneha
    March 21, 2012
    Reply

    hello,
    Please will you elaborate the next step by which I can show list of contact like what sh I write in JS so that I can use it.

    kindly help.

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

This blog post will guide you through several effective methods to troubleshoot and resolve the issue of Microsoft Edge not...
Windows 11 offers a range of audio enhancements that can enrich your listening experience. These enhancements include features like virtual...
Windows 11 brings a fresh and visually stunning design to your desktop, and one of the standout features is the...