Windows phone 8.1 using Contact Picker to get 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 the phone number, the "contact" object does not contain email addresses, even if they are present in the contact information. One option is to switch:

   ContactPicker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.PhoneNumber);

      

from

   ContactPicker.DesiredFieldsWithContactFieldType.Add(ContactFieldType.Email);

      

But then I don't get the phone number ... I want to get all the information in one of them.

Is there a way to select any information with a single selection? (I also tried adding multiple entries to DesiredFieldsWithContactFieldType, but then I get an exception ...)

Respectfully,

Keran

EDIT 08/07/2015: Since "ContactPicker.DesiredFieldsWithContactFieldType" can only accept one "ContactFieldType", the way I worked around this was by first allowing the user to get contacts by ContactFieldType.PhoneNumber, and then I was programmatically extracting the email address of the selected contacts.

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

+3


source to share


1 answer


Try the following:

  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, first you need to get the "skeleton" of the contact, and then you can get the entire contact entity with all its properties from the ContactStore object.



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

Hope it helps.

+1


source







All Articles