Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Search phone number in phonebook

From Wiki
Jump to: navigation, search
Article Metadata
Code ExampleArticle
Created: kiranmudiyam (13 Sep 2007)
Last edited: hamishwillee (30 May 2013)

The class CContactDatabase provides MatchPhoneNumberL method to search for phone numbers in telephone, fax or SMS type fields of phonebook contact item.

This is useful in telephony applications to find the presence of an incoming call phone number in the contacts.

The following code snippet searches for the given phone number and displays all the fields information of all the contacts matched.

Header Files:

#include <CNTFLDST.H>
#include <CNTITEM.H>

Link against:

LIBRARY cntmodel.lib

Capabilities Required:

void SearchPhoneNumber()
// phone number to search
TBuf<128> callNameBuf;
// Opens the default contact database
iContactsDb = CContactDatabase::OpenL();
// Search for all phonebook items matching phone number
iMyIdArray= iContactsDb->MatchPhoneNumberL(KToken,10);
// Count of matchings found
CEikonEnv::InfoWinL(_L("Count of Matches"),callNameBuf);
// Reading matched contacts info
for(TInt i = 0;i < iMyIdArray->Count();i++)
TContactItemId id = (*iMyIdArray)[i];
// Read contact using id
CContactItem* contact = iContactsDb->ReadContactL(id);
// Get a reference to the contact item's field set
CContactItemFieldSet& fieldSet = contact->CardFields();
// Read and display all fields
for ( TInt j = 0 ; j < fieldSet.Count() ; j++ )
const CContactItemField& field = fieldSet[j] ;
// phone numbers are stored in database using text fields

NOTE: The comparison method used is not exact. The number is compared starting from the right side of the number and the method returns an array of candidate matches.It is recommended that at least 7 match digits are specified even when matching a number containing fewer digits. Punctuation (eg. spaces) and other alphabetic characters are ignored when comparing.So the search will be successful even if phone number has spaces.

_LIT(KToken,"+9199 083 44484"); // phone number with spaces
iMyIdArray= iContactsDb->MatchPhoneNumberL(KToken,15);

Code Example

This page was last modified on 30 May 2013, at 04:40.
49 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.