Accessing Contacts Information in Windows 8.1

Windows 8.1 SDK has lots of updates and changes compared to Windows 8 SDK. As an epitome of all these changes most of the Windows 8 SDK methods and classes have been deprecated and not advised to be used in the new development.

This post shows how to access the Contacts information in Windows 8.1 and 8. The new SDK is easy to use for sure and more sleek.

 Accessing Contacts Information using Windows 8 SDK

   1: private async void GetContactsDataUsingWindowd8SDK()

   2: {

   3:     var contactPicker = new Windows.ApplicationModel.Contacts.ContactPicker();

   4:     contactPicker.CommitButtonText = "Select a contact";

   5:  

   6:     ContactInformation contact = await contactPicker.PickSingleContactAsync();

   7:  

   8:     // For multiple contacts Selection

   9:     //IReadOnlyList<ContactInformation> contacts = await contactPicker.PickMultipleContactsAsync();

  10:  

  11:     var viewmodel = new ContactViewModel();

  12:  

  13:     if (contact != null)

  14:     {

  15:         string name = contact.Name;

  16:  

  17:         if (contact.Emails.Count > 0)

  18:         {

  19:             StringBuilder sb = new StringBuilder();

  20:             foreach (IContactField field in contact.Emails)

  21:             {

  22:                 sb.AppendFormat("{0} ({1}) \n", field.Value, field.Category);

  23:             }

  24:             string email = sb.ToString();

  25:         }

  26:  

  27:         // Display the contact’s thumbnail

  28:         Windows.Storage.Streams.IRandomAccessStreamWithContentType stream = await contact.GetThumbnailAsync();

  29:  

  30:         if (stream != null && stream.Size > 0)

  31:         {

  32:             BitmapImage image = new BitmapImage();

  33:             image.SetSource(stream);

  34:         }

  35:     } 

 

Same to be done in Windows 8.1 SDK

   1: private async void GetContactInfoUsingWindows81SDK()

   2: {

   3:     var contactPicker = new Windows.ApplicationModel.Contacts.ContactPicker();

   4:     contactPicker.CommitButtonText = "Select Contacts";

   5:  

   6:     // select specific properties only

   7:     contactPicker.SelectionMode = Windows.ApplicationModel.Contacts.ContactSelectionMode.Fields;

   8:  

   9:     //contactPicker.DesiredFieldsWithContactFieldType.Add();

  10:  

  11:     var contactCollection = await contactPicker.PickContactsAsync();

  12:     //contactCollection.Add(new Windows.ApplicationModel.Contacts.Contact());

  13:  

  14:     List<ContactViewModel> contactViewModels = new List<ContactViewModel>();

  15:  

  16:     if (contactCollection != null)

  17:     {

  18:         foreach (var contact in contactCollection)

  19:         {

  20:             var viewmodel = new ContactViewModel();

  21:  

  22:             viewmodel.Id = contact.Id;

  23:             viewmodel.FullName = contact.FirstName + " " + contact.LastName;

  24:  

  25:             if (contact.Emails.Any())

  26:             {

  27:                 foreach (var email in contact.Emails)

  28:                 {

  29:                     if (email.Kind == Windows.ApplicationModel.Contacts.ContactEmailKind.Personal)

  30:                     {

  31:                         viewmodel.PersonalEmail = email.Address;

  32:                     }

  33:                 }

  34:             }

  35:  

  36:             var thumbnail = await contact.Thumbnail.OpenReadAsync();

  37:  

  38:             if (thumbnail != null && thumbnail.Size > 0)

  39:             {

  40:                 viewmodel.ContactThumbnail.SetSource(thumbnail);

  41:             }

  42:  

  43:  

  44:         }

  45:     }

  46: }

 

Code for the View Model class used in 8.1

   1: public class ContactViewModel

   2: {

   3:     public string Id { get; set; }

   4:  

   5:     public string FullName { get; set; }

   6:  

   7:     public string PersonalEmail { get; set; }

   8:  

   9:     public BitmapImage ContactThumbnail { get; set; }

  10: }

You can notice that 8.1 version the methods have changes and enums have been introduced to make the development more concise.

Advertisements

One thought on “Accessing Contacts Information in Windows 8.1

  1. Its very well written; I love what youve got to say. But maybe you could a little more in the
    way of content so people could connect with it better.

Comments are closed.

Powered by WordPress.com.

Up ↑

%d bloggers like this: