contacts.find

Queries the device contacts database and returns one or more [Contact](Contact/contact.html) objects, each containing the fields specified.

navigator.contacts.find(contactFields, contactSuccess, contactError, contactFindOptions);

Description

contacts.find is an asynchronous function that queries the device contacts database and returns an array of [Contact](Contact/contact.html) objects. The resulting objects are passed to the [contactSuccess](parameters/contactSuccess.html) callback function specified by the contactSuccess parameter.

Users must specify the contact fields to be used as a search qualifier in the contactFields parameter. Only the fields specified in the contactFields parameter will be returned as properties of the [Contact](Contact/contact.html) objects that are passed to the contactSuccess callback function. A zero-length contactFields parameter is invalid and will result in a [ContactError](ContactError/contactError.html).INVALID_ARGUMENT_ERROR . A contactFields value of ["*"] will return all contact fields.

The contactFindOptions.filter string can be used as a search filter when querying the contacts database. If provided, a case-insensitive, partial value match is applied to each field specified in the contactFields parameter. If a match is found in a comparison with any of the specified fields, the contact is returned.

Parameters

  • contactFields: Contact fields to be used as search qualifier. Only these fields will have values in the resulting [Contact](Contact/contact.html) objects. (DOMString[]) [Required]
  • contactSuccess: Success callback function that is invoked with the contacts returned from the contacts database. [Required]
  • contactError: Error callback function. Invoked when error occurs. [Optional]
  • contactFindOptions: Search options to filter contacts. [Optional]

Supported Platforms

  • Android
  • BlackBerry WebWorks (OS 5.0 and higher)
  • iOS
  • Windows Phone 7 and 8
  • Bada 1.2 & 2.0
  • Windows 8

Quick Example

function onSuccess(contacts) {
    alert('Found ' + contacts.length + ' contacts.');
};

function onError(contactError) {
    alert('onError!');
};

// find all contacts with 'Bob' in any name field
var options = new ContactFindOptions();
options.filter="Bob";
options.multiple=true; 
var fields = ["displayName", "name"];
navigator.contacts.find(fields, onSuccess, onError, options);

Full Example

<!DOCTYPE html>
<html>
  <head>
    <title>Contact Example</title>

    <script type="text/javascript" charset="utf-8" src="cordova-2.5.0.js"></script>
    <script type="text/javascript" charset="utf-8">

    // Wait for Cordova to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // Cordova is ready
    //
    function onDeviceReady() {
	    // find all contacts with 'Bob' in any name field
	    var options = new [ContactFindOptions](ContactFindOptions/contactfindoptions.html)();
		options.filter="Bob"; 
		var fields = ["displayName", "name"];
	    navigator.contacts.find(fields, onSuccess, onError, options);
    }

    // onSuccess: Get a snapshot of the current contacts
    //
    function onSuccess(contacts) {
		for (var i=0; i<contacts.length; i++) {
			console.log("Display Name = " + contacts[i].displayName);
		}
    }

    // onError: Failed to get the contacts
    //
    function onError(contactError) {
        alert('onError!');
    }

    </script>
  </head>
  <body>
    <h1>Example</h1>
    <p>Find Contacts</p>
  </body>
</html>