Windows Phone Mango–What’s New? (“Local Data” – Part 2 of 8)

May 24, 2011

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:


image


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”


Alex

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

9 comments

  1. highsenseMay 26, 2011 ב 5:13

    hi bro.Does the new contacts api provide the setter interface since my app could add or update the contacts?

    Reply
  2. Alex GoleshMay 26, 2011 ב 5:51

    highsense — no, the API is read-only.

    //Alex

    Reply
  3. matroMay 26, 2011 ב 12:32

    hi Alex,

    thank you for the sample. Just a question: if the appointments database is empty on the emulator, and there is no Mango for physical devices, how can we test our interactions with appointments?

    Reply
  4. Alex GoleshMay 26, 2011 ב 12:59

    matro:
    Currently is little bit problematic :)

    Reply
  5. brunomanJuly 8, 2011 ב 14:12

    readonly ??? they’ve got to be kidding :(

    Reply
  6. Praveen GaddamAugust 10, 2011 ב 14:15

    I am new WP7 Development .I have retreived Contact from third party API now i want to add that contacts to Emulator contacts ..

    Please guide me

    Reply
  7. Alex GoleshAugust 15, 2011 ב 11:47

    In Windows Phone Mango Beta2 and up you can use SaveContact task to save the contacts. In emulator it will exists as long as you don’t close the emulator instance

    Best,
    Alex

    Reply
  8. Kiran SubhedarSeptember 7, 2011 ב 2:58

    Is there a way to create an appointment/task through code?

    Reply
  9. Alex GoleshSeptember 7, 2011 ב 19:38

    Kiran Subhedar: I’m afraid no.

    Reply