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; }

}
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

11 Comments

  1. Manjunath June 1, 2011 Reply
  2. Dare June 2, 2011 Reply
    • Senthil Kumar June 2, 2011 Reply
  3. Dare June 2, 2011 Reply
    • Senthil Kumar June 18, 2011 Reply
  4. developer October 31, 2011 Reply
  5. malik umar hassan December 5, 2011 Reply
  6. Sneha March 21, 2012 Reply

Leave a Reply


x

Interesting BlogPosts to read

Download - Pokemon Go for for Windows 10 (Third Party app)
Pokemon Go is one of the popular AR games for android and iOS devices and now , the game is made ava...
Open Wordpad from command line in Windows
You can use the command "Write" from command line in Windows to open the WordPad application.D:\>...
Popular cross-platform (android and iOS) to-do list app
In this blog post, we will cover some of the popular to-do list app that is available on different p...