Alfred Francis
Alfred Francis

Reputation: 451

How to list all contacts from BlackBerry-10 contacts list?

I am creating an SMS chat application for blackberry 10.So i want list list all contacts from blackberry database and list it to user so that user can easily select recipient's mobile numbers easily.Is there any way to do it using html5/webworks.I just want get all contact's name and mobile number into an array or something like that.Anyone can help ?? Thanks in advance.

https://developer.blackberry.com/html5/apis/blackberry.pim.contacts.contact.html

Upvotes: 0

Views: 685

Answers (1)

techsaint
techsaint

Reputation: 772

BB10 Has strict UI guidelines. You can do this two ways. You can invoke the already existing native Contact List "Card" or call the find API directly.

To Invoke the Contact List card, use the invokeContactPicker invocation pattern. The full sample code is on the blackberry developer site https://developer.blackberry.com/html5/apis/blackberry.pim.contacts.html#.invokeContactPicker, but here are the important snippets for invoking a single selection Card (you can invoke single, multiple and attribute selection):

function onCancel() {
    alert("User pressed cancel in contact picker.");
}

function onInvoke(error) {
    if (error) {
       alert("Error invoking contact picker: " + error.code);
    } else {
       alert("Contact picker invoked!");
    }
}

function onContactSelected(data) {
   var contact = contacts.getContact(data.contactId);
   if (contact) {
      alert("Contact id #" + contactId + " corresponds to '" + contact.name.givenName + " " + contact.name.familyName +"'.");
   } else {
      alert("There is no contact with id: " + contactId);
   }

}

function onContactsSelected(data) {
    alert("Total # contacts selected: " + data.contactIds.length);
}
function invokeContactPickerSingle() {
    contacts.invokeContactPicker({
        mode: ContactPickerOptions.MODE_SINGLE,
        fields: ["phoneNumbers"]
    }, onContactSelected, onCancel, onInvoke);
}

To gather contacts and process them directly, use the blackberry.pim.contacts.find API. The full sample code is on the blackberry site, here: https://developer.blackberry.com/html5/apis/blackberry.pim.contacts.html#.find but below is a snippet of the relevant code:

function listAllContacts() {
    var sort = [{
             "fieldName": ContactFindOptions.SORT_FIELD_FAMILY_NAME,
             "desc": false
        }, {
             "fieldName": ContactFindOptions.SORT_FIELD_GIVEN_NAME,
             "desc": true
        }],
        // no filter - return all contacts
        findOptions = { 
             // sort contacts first by family name (desc), then by given name (asc)
             sort: sort,  
             limit: -1 // limit - all contacts returned
        };
    contacts.find(["name"], findOptions, onFindSuccess, onFindError);
}
function onFindSuccess(results) {
    console.log("Found " + results.length + " contacts in total");
}

function onFindError(error) {
    console.log("Error: " + error.code);
}

Let me know if this helps out!

Upvotes: 1

Related Questions