7

From my app I create contacts using StoredContact and the ContactStore, setting the mobile phone number using KnwonContactProperties.MobileTelephone via GetPropertiesAsync.

This is fine, and I can see the mobile phone number in People.

But...

If I try to access the contacts programatically via ContactManager.RequestStoreAsync, I don't see this phone number in the contact.Phones collection.

Is there any way to get numbers written into the Phones collection?

(Related question)

Community
  • 1
  • 1
Benjol
  • 63,995
  • 54
  • 186
  • 268

1 Answers1

1

The KnownContactProperties class is in under Windows.Phone.PhoneContractnamespace, but ContactManager.RequestStoreAsync() is under Windows.ApplicationModel.Contacts namespace. It may be the reason you cannot get the phone numbers. ContactStore.CreateOrOpenAsync method under Windows.Phone.PhoneContract same with KnownContactProperties can work well. Here is a completed demo for inserting a contact and then get the contact's name and phone number.

XAML Code

<StackPanel>
    <TextBox x:Name="txtName" Header="name" InputScope="NameOrPhoneNumber"/>
    <TextBox x:Name="txtTel" Header="phone number 1" InputScope="ChineseHalfWidth"/>
    <TextBox x:Name="txtTel1" Header="phone number 2" InputScope="TelephoneNumber"/>
    <Button x:Name="btnSave" Content="Save" Click="btnSave_Click"/>
    <Button x:Name="btnGet" Content="GET" Click="btnGet_Click"/> 
</StackPanel>

Code behind

 private async void btnSave_Click(object sender, RoutedEventArgs e)
 {
     var name = txtName.Text;
     var tel = txtTel.Text;

     ContactStore contactStore = await ContactStore.CreateOrOpenAsync(ContactStoreSystemAccessMode.ReadWrite, ContactStoreApplicationAccessMode.ReadOnly);
     ContactInformation contactInformation = new ContactInformation();
     contactInformation.DisplayName = name;
     var contactProps = await contactInformation.GetPropertiesAsync();
     contactProps.Add(KnownContactProperties.MobileTelephone, tel);
     StoredContact storedContact = new StoredContact(contactStore, contactInformation);
     await storedContact.SaveAsync();
 }

 private async void btnGet_Click(object sender, RoutedEventArgs e)
 {

     ContactStore contactStore = await ContactStore.CreateOrOpenAsync(ContactStoreSystemAccessMode.ReadWrite, ContactStoreApplicationAccessMode.ReadOnly);
     var result = contactStore.CreateContactQuery();
     var count = await result.GetContactCountAsync();
     var list = await result.GetContactsAsync();
     foreach (var item in list)
     {
         var properties = await item.GetPropertiesAsync();
         System.Diagnostics.Debug.WriteLine(item.DisplayName);                
         System.Diagnostics.Debug.WriteLine(properties[KnownContactProperties.MobileTelephone].ToString());
     }
 }
Sunteen Wu
  • 10,509
  • 1
  • 10
  • 21
  • Hm, thanks for trying. Unfortunately what I need to do is the opposite: be able to write to `Windows.ApplicationModel.Contacts`, or at least find someway of writing into the `.Phones` property of the Contact there. The reason is that (as far as I can tell) *that* is where WhatsApp is looking, and I want WhatsApp to be able to detect the contacts that my App is creating... – Benjol Sep 02 '16 at 13:00
  • Have you checked this official sample:https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/ContactCardIntegration. This sample have code about writing contact to `windows.ApplicationModel.Contacts`, did you have problems when using it ? It use [contactphone class](https://msdn.microsoft.com/en-us/library/windows/apps/windows.applicationmodel.contacts.contactphone) – Sunteen Wu Sep 05 '16 at 04:53
  • Actually I think I may misunderstand you. You set the phone property with `KnwonContactProperties.MobileTelephone ` which is come from `Windows.Phone.PhoneContract`. But now you said you want to set the phone property with `windows.ApplicationModel.Contacts`. Have you test my code? The code list the contacts created by the app. – Sunteen Wu Sep 05 '16 at 04:59
  • My app (which started on 8.1) currently writes using `Windows.Phone`, and the contacts and numbers show up fine in People. But WhatsApp can't 'see' the phone numbers for some reason. I created a contact directly in People, and WhatsApp could see the number. Subsequently, using `Windows.ApplicationModel.Contacts` I saw that the number was visible on the contact. Thus I suppose that that is where WhatsApp is looking, so I want to write there. I've seen your link, but it says Win 10 only, I hope that's just for building, and doesn't mean it won't work on 8.1... – Benjol Sep 05 '16 at 12:45
  • Why you say WhatsApp can't 'see' the phone numbers for some reason.? In my code, I print out the phone number result in console. My code sample can work on 8.1 as well as 10 – Sunteen Wu Sep 06 '16 at 08:54
  • Sunteen, I have no idea, all I know is that if you have a contact created by the Windows.Phone.PhoneContract route, WhatsApp will see the contact, but not the number (on my Win 10 in any case). Of course this is most probably a bug in WhatsApp, but I can't do anything about that! – Benjol Sep 06 '16 at 09:21
  • Unfortunately it looks like the github example is effectively win10 only :( – Benjol Sep 06 '16 at 11:23