DCSIMG
Windows Phone Mango–What’s New? (“Local Data” - Part 2 of 8) - Alex Golesh's Blog About Silverlight Development

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

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

Comments

# The Morning Brew - Chris Alcock &raquo; The Morning Brew #859

Pingback from  The Morning Brew - Chris Alcock  &raquo; The Morning Brew #859

Wednesday, May 25, 2011 10:45 AM by The Morning Brew - Chris Alcock » The Morning Brew #859

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

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

Thursday, May 26, 2011 5:13 AM by highsense

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

highsense -- no, the API is read-only.

//Alex

Thursday, May 26, 2011 5:51 AM by Alex Golesh

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

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?

Thursday, May 26, 2011 12:32 PM by matro

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

matro:

Currently is little bit problematic :)

Thursday, May 26, 2011 12:59 PM by Alex Golesh

# Alex Golesh's Windows Phone Mango – What’s New Series | Ginktage

Pingback from  Alex Golesh's Windows Phone Mango – What’s New Series | Ginktage

# Alex Golesh's Windows Phone Mango – What’s New Series | Ginktage

Pingback from  Alex Golesh's Windows Phone Mango – What’s New Series | Ginktage

# Dev Dude&#8217;s Guide to Mango Series &#8211; &#8220;Windows Phone Mango???What???s New?&#8221; in 8 parts (Think &#8220;Code trucks filled with many Mango&#8217;s&#8221;) | gong

Pingback from  Dev Dude&#8217;s Guide to Mango Series &#8211; &#8220;Windows Phone Mango???What???s New?&#8221; in 8 parts (Think &#8220;Code trucks filled with many Mango&#8217;s&#8221;) | gong

# Was ist neu im Mango SDK | Biggle&#039;s Blog

Pingback from  Was ist neu im Mango SDK | Biggle&#039;s Blog

Thursday, June 09, 2011 9:52 AM by Was ist neu im Mango SDK | Biggle's Blog

# Windows Phone Mango – New Tasks (Beta & Beta2)

Windows Phone Mango adds few more task over initial Windows Phone (RTM) release. This post will cover

Wednesday, June 29, 2011 6:57 PM by Alex Golesh's Blog About Silverlight Development

# Windows Phone Mango – New Tasks (Beta & Beta2)

Windows Phone Mango adds few more task over initial Windows Phone (RTM) release. This post will cover

Wednesday, June 29, 2011 7:45 PM by Community Blogs

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

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

Friday, July 08, 2011 2:12 PM by brunoman

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

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

Wednesday, August 10, 2011 2:15 PM by Praveen Gaddam

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

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

Monday, August 15, 2011 11:47 AM by Alex Golesh

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

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

Wednesday, September 07, 2011 2:58 AM by Kiran Subhedar

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

Kiran Subhedar: I'm afraid no.

Wednesday, September 07, 2011 7:38 PM by Alex Golesh

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: