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. Thanks for all your past and future contributions.

Revision as of 03:29, 5 July 2012 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

How to search Mobile number from Contacts using WRT

From Wiki
Jump to: navigation, search
Article Metadata
Tested with
Devices(s): Nokia 5230
Created: girishpadia (05 Mar 2011)
Last edited: hamishwillee (05 Jul 2012)



This is a small utility application to search the part of mobile number from the contact list. The application uses Contacts API of Web Runtime. This is based on the Web Runtime code example available at CS001235 - Retrieving contacts info in WRT and further modified.

How the application works

The application uses WRT Contacts API. It reads all contact one by one and compare each Mobile Number from the contact list with the search string entered by user. If the search matches with the mobile number, the contact's Firstname, Lastname and Mobile number will be displayed on the screen. If search does not match then the contact will not be displayed.

How to create project

Please download and install the Nokia Web SDK. Create a new project in it with name phonebook. Replace the files, phonebook.html,phonebook.css and phonebook.js with the code below.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="">
<script type='text/javascript' src="phonebook.js"></script>
<link rel="stylesheet" href="phonebook.css" type="text/css" />
<title>Search Mobile Number</title>
<body onload="init()">
<!-- input field for search string -->
<input id="searchstring" type="text" size=20 maxlength=10 />
<input type='button' onclick='doRefresh()' value='Search' />
<!-- Table for displaying contacts info -->
<table id="maintable" cellspacing='1' cellpadding='1' border='1'>


/*The following style sheet applies to the table headings.*/
table th{
/* The following style sheet applies to the table whenever you hover on it. The background color will change to Pink */
#maintable tr:hover {
/* Following style sheet applies to the input type=text field */
border: 1px solid #000000;
font-size: 12px;


// contacts system service object
var phonebook;
// system error code
var noItemsErrorCode = 1012;
//Menu command id, all of menu items must have unique id
var CMD_SEARCH = 11;
* Setting default data on application load

function init() {
// getting contacts system service object
try {
phonebook = device.getServiceObject("Service.Contact", "IDataSource");
} catch(err) {
alert( "No Contact service available" );
// Create main menu items
var searchData = new MenuItem("Search", CMD_SEARCH);
// Setting handlers for main menu items
searchData.onSelect = doRefresh;
// Adding items to menu
// Start displaying menu
// Refreshing info
* function refreshes information in table

function doRefresh() {
// Criteria object for making a GetList call to receive
// contacts information
var criteria = new Object();
criteria.Type = 'Contact';
var temp = document.getElementById('searchstring').value;
// if(temp != "") {
// if we have some search string add search parameter to criteria
// criteria.Filter = new Object();
// criteria.Filter.SearchVal = temp;
// }
// making a call to service to receive a list of suitable items
try {
contacts = phonebook.IDataSource.GetList(criteria);
} catch(err) {
alert( "Error retrieving contacts information" );
// receiving a handler to table to display info
var table = document.getElementById('maintable');
// cleaning table and adding header
while(table.rows[0] != undefined) {
table.rows[0].innerHTML = "<th>Firstname</th><th>Lastname</th>" +
// looking for error code returned by GetList
if(contacts.ErrorCode != 0) {
// if error code is not "no suitable items" --
// show alert otherwise leave the function
if(contacts.ErrorCode != noItemsErrorCode) {
} else {
// filling table with received data
var table = document.getElementById('maintable');
var i=0; // iterator
var item; // pointer to item in received list
// The while loop initialized as an infinite loop
while(true) {
if((item = contacts.ReturnValue.getNext()) == undefined)
break; // The while loop will break as soon as all contacts are fetched.
if (item.MobilePhoneGen != undefined) {
if ( != -1 || temp == "") {
table.insertRow(i + 1);
var result = "<td>";
if (item.FirstName != undefined) {
result += item.FirstName.Value;
result += "</td><td>";
if (item.LastName != undefined) {
result += item.LastName.Value;
result += "</td><td>";
if (item.MobilePhoneGen != undefined) {
result += item.MobilePhoneGen.Value;
result += "</td>";
table.rows[i + 1].innerHTML = result;

Tested On device

The game is tested on Nokie 5230

Application Image

Article 9 image 1.jpg

Source Code download


This page was last modified on 5 July 2012, at 03:29.
68 page views in the last 30 days.