Namespaces

Variants
Actions

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.

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
Article
Created: girishpadia (05 Mar 2011)
Last edited: hamishwillee (05 Jul 2012)

Contents

Introduction

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.


phonebook.html

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type='text/javascript' src="phonebook.js"></script>
<link rel="stylesheet" href="phonebook.css" type="text/css" />
<title>Search Mobile Number</title>
</head>
<body onload="init()">
<!-- input field for search string -->
<div>
<input id="searchstring" type="text" size=20 maxlength=10 />
<input type='button' onclick='doRefresh()' value='Search' />
</div>
<!-- Table for displaying contacts info -->
<table id="maintable" cellspacing='1' cellpadding='1' border='1'>
<tr><th>Firstname</th><th>Lastname</th><th>Phone</th></tr>
</table>
</body>
</html>


phonebook.css

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


basic.js

// 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" );
return;
}
 
// Create main menu items
var searchData = new MenuItem("Search", CMD_SEARCH);
 
// Setting handlers for main menu items
searchData.onSelect = doRefresh;
 
// Adding items to menu
menu.append(searchData);
 
// Start displaying menu
menu.showSoftkeys();
 
// Refreshing info
doRefresh();
}
 
/**
* 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" );
return;
}
// receiving a handler to table to display info
var table = document.getElementById('maintable');
// cleaning table and adding header
while(table.rows[0] != undefined) {
table.deleteRow(0);
}
table.insertRow(0);
table.rows[0].innerHTML = "<th>Firstname</th><th>Lastname</th>" +
"<th>Phone</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) {
alert(contacts.ErrorMessage);
}
return;
} 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) {
mobileno=item.MobilePhoneGen.Value;
if (mobileno.search(temp) != -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

File:Search Mobile.zip

This page was last modified on 5 July 2012, at 03:29.
104 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.

×