Mango provides API to use user’s Contacts and Appointments. To search for appointments and contacts we need to use the Appointments and Contacts classes located under Microsoft.Phone.UserData namespace.
In this post I’ll show how to create the sample application which will present the list of contacts and will enable user searching for specific contact. Also we will add appointments search functionality:
When page initialized we creating the new instance of the Contacts class:
contacts = new Contacts();
contacts.SearchCompleted += contacts_SearchCompleted;
The contacts class contains information which indicates the accounts used to provide the information. The information provided via Accounts property. It holds a collection of accounts with account kind (of StorageKind type) and account name.
In our sample the SearchCompleted event handler groups the results according to the first letter to enable binding for the presentation:
void contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
// Group all contacts according to the first letter in their display name
var itemsSource = e.Results.GroupBy(c => c.DisplayName.First()).
OrderBy(group => group.Key).
Select(group => new ContactsGroup(group));
The ContactsSearchEventArgs returns also the filter object which could be used to search for the results. Each item in Results collections holds an instance of Contact object from Microsoft.Phone.UserData namespace. It has the all the information about the contact (such as PhoneNumbers, DisplayName, EmailAddresses, Companies), the Accounts associated with this contact and indicates whether the contact is pinned to the start screen or not (IsPinnedToStart).
To start the search we need to execute the SearchAsyc method. This method asynchronously searches for contacts in the user’s contact data:
contacts.SearchAsync(“Alex”, FilterKind.DisplayName, null);
Method parameters specify the search query (“Alex”) and search filter (FilterKind.DisplayName) which indicates which field will be used to search.
Similarly to the Contacts, Windows Phone API provides the Appointments class in Microsoft.Phone.UserData namespace to search for user’s appointments. Appointments class initialized similarly to the Contacts class:
appointments = new Microsoft.Phone.UserData.Appointments();
appointments.SearchCompleted += new EventHandler<AppointmentsSearchEventArgs>(appointments_SearchCompleted);
To start the search, invoke the SearchAsyc method which asynchronously searches for appointments that occur between the specified start date and time and end date and time.
appointments.SearchAsync(DateTime.Now, DateTime.Now.AddDays(5), null);
The code snippet searches for all appointments in all accounts in next 5 days.
The SearchCompleted event handler returns also Start and End date/time used to search for current result set. Each item in Results collections holds an instance of Appointment object from Microsoft.Phone.UserData namespace. It has the all the information about the appointment (such as Location, Organizer, Atrendees list, Subject, etc.).
Important note: you can use Windows Phone Emulator to develop only Contacts-related scenarios because the emulator has the list of sample contacts. Appointments-related scenarios are not supported on emulator and require a real device.
The sample application code hosted here.
Stay tuned for part 3 – “New Sensors & Tools Enhancements”