Jaa


Xamarin.Essentials: Contacts

The Contacts class lets a user pick a contact and retrieve information about it.

Get started

To start using this API, read the getting started guide for Xamarin.Essentials to ensure the library is properly installed and set up in your projects.

To access the Contacts functionality the following platform specific setup is required.

The ReadContacts permission is required and must be configured in the Android project. This can be added in the following ways:

Open the AssemblyInfo.cs file under the Properties folder and add:

[assembly: UsesPermission(Android.Manifest.Permission.ReadContacts)]

OR Update Android Manifest:

Open the AndroidManifest.xml file under the Properties folder and add the following inside of the manifest node.

<uses-permission android:name="android.permission.READ_CONTACTS" /> />

Or right click on the Android project and open the project's properties. Under Android Manifest find the Required permissions: area and check this permission. This will automatically update the AndroidManifest.xml file.

Pick a contact

By calling Contacts.PickContactAsync() the contact dialog will appear and allow the user to receive information about the user.

try
{
    var contact = await Contacts.PickContactAsync();

    if(contact == null)
        return;

    var id = contact.Id;
    var namePrefix = contact.NamePrefix;
    var givenName = contact.GivenName;
    var middleName = contact.MiddleName;
    var familyName = contact.FamilyName;
    var nameSuffix = contact.NameSuffix;
    var displayName = contact.DisplayName;
    var phones = contact.Phones; // List of phone numbers
    var emails = contact.Emails; // List of email addresses
}
catch (Exception ex)
{
    // Handle exception here.
}

Get all contacts

ObservableCollection<Contact> contactsCollect = new ObservableCollection<Contact>();

try
{
    // cancellationToken parameter is optional
    var cancellationToken = default(CancellationToken);
    var contacts = await Contacts.GetAllAsync(cancellationToken);

    if (contacts == null)
        return;

    foreach (var contact in contacts)
        contactsCollect.Add(contact);
}
catch (Exception ex)
{
    // Handle exception here.
}

Platform differences

  • The cancellationToken parameter in the GetAllAsync method is only used on UWP.

API