windows-phone-8.1contactpicker

Windows phone 8.1 using Contact Picker to retrieve both email and phone number


I am using the following code to allow the user to select contacts:

ContactPicker ContactPicker = new ContactPicker();

        ContactPicker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.PhoneNumber);

        var Contacts = await ContactPicker.PickContactsAsync();


        if (Contacts.Count > 0)
        {
            foreach (Contact contact in Contacts)
            {
                string telephone = string.Empty;
                string email = string.Empty;
                if (contact.Phones.Count > 0)
                {
                    telephone = contact.Phones[0].Number;
                }
                if (contact.Emails.Count > 0)
                {
                    email = contact.Emails[0].Address;
                }
                PartyPerson person = new PartyPerson(DateTime.Now.ToString("PP_yyMMdd_hhmmss_ffff"), true, contact.DisplayName, 0, 0, 0, email, telephone);
                AddPartyPerson(person);
            }
        }
        ContactPicker = null;

However, I only get phone number, the object "contact" does not contain any email addresses even though they are present in the contact information. One option is to switch:

   ContactPicker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.PhoneNumber);

with

   ContactPicker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.Email);

But then I don't get a phone number... I want to be able to retrieve all the information in one select.

Is there any way to select both information via one select? (I also tried adding more than one entry to DesiredFieldsWithContactFieldType but then I get an exception...)

Best regards,

Keran

EDIT 07.08.2015: Since the "ContactPicker.DesiredFieldsWithContactFieldType" can only accept one type of "ContactFieldType", the way I worked around this was first allow the user to get the contacts by ContactFieldType.PhoneNumber and then I programatically retrieve the email addressess of the selected contacts.

From the users point of view, this won't be a problem since everything will be visible correctly in the ContactPicker.PickContactsAsync, we just need to retrieve the missing email information manually in code-behind, which is easy since we know what contacts were selected by the user.


Solution

  • Try this:

      ContactStore contactStore = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AllContactsReadOnly);
    
      ContactPicker contactPicker = new ContactPicker();
      IList<Contact> pickedContacts = await contactPicker.PickContactsAsync();
      int pickedCount = pickedContacts != null ? pickedContacts.Count : 0;
      if (pickedCount > 0)
      {
           for (int i = 0; i < count; ++i)
           {
                 Contact c = pickedContacts[i];
                 Contact realContact = await contactStore.GetContactByIdAsync(c.Id);
                 //...
           }
      }
    

    So, you first need to get the "skeleton" of the contact, and then you can get the whole contact entity with all its properties from the ContactStore object.

    It works for me on Windows 10 Mobile. There shouldn't be much difference from Windows 8.

    Hope it helps.