Friday, June 17, 2011

Access PIM information (contacts) from Windows phone7

As a update to Windows phone , Mango SDK comes along with new set of API's that includes accessing PIM too. So we can access contacts easily using new API's
Now here is sample code below for accessing the contact information.

First we need to include namespace
using Microsoft.Phone.UserData;

 public MainPage()
        {
            InitializeComponent();
            Contacts myContacts = new Contacts();
            //   Sample Code to save contacts on phone.
            //SavePhoneNumberTask savePhoneNumberTask = new SavePhoneNumberTask();
            //savePhoneNumberTask.PhoneNumber = "1234567890"; 
            
            myContacts.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(myContacts_SearchCompleted);

            myContacts.SearchAsync(string.Empty, FilterKind.None, null);
        }

void myContacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
        {
            try
            {
                string strContacts = string.Empty;
                foreach (var result in e.Results)
                {
                  strContacts = strContacts + "Name : " + result.DisplayName + " ; Phone Number : " + result.PhoneNumbers.FirstOrDefault();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

No comments:

Post a Comment