Search code examples
xamarinwindows-phoneuwpcontactswindows-10-mobile

Get the list of Contacts on Windows 10 Phone


I am needing to know how to read the contact list on a Windows 10 Phone. I don't want to use a contact picker; I just need to be able to iterate through all contacts to access their name and phone number, and store it in a List. (Similar to how WhatsApp is able to read your contact list and display it in their app)


Solution

  • I've taken some code from another answer here.

    With this code you should be able to get the contacts out.

    public async Task IterateThroughContactsForContactListId()
     {            
                ContactStore allAccessStore = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AllContactsReadOnly);
    
                var contacts = await allAccessStore.FindContactsAsync();
                foreach (var contact in contacts)
                {
                    //process aggregated contacts
                    if (contact.IsAggregate)
                    {
                        //here contact.ContactListId is "" (null....)                  
                        //in this case if you need the the ContactListId then you need to iterate through the raw contacts
                        var rawContacts = await allAccessStore.AggregateContactManager.FindRawContactsAsync(contact);
                        foreach (var rawContact in rawContacts)
                        {
                            //Here you should have ContactListId
                            Debug.WriteLine($"aggregated, name: {rawContact.DisplayName }, ContactListId: {rawContact.ContactListId}");
                        }
                    }
                    else //not aggregated contacts should work
                    {
                        Debug.WriteLine($"not aggregated, name: {contact.DisplayName }, ContactListId: {contact.ContactListId}");
                    }
                }
    
    }
    

    He also notes:

    And very important: In the appxmanifest you have to add the contacts capability. Right click to it in the solution explorer and "View Code" and then under Capabilities put

    <uap:Capability Name="contacts" />
    

    There is no UI for this. See this.