| ID | CS001240 | Creation date | December 18, 2008 |
| Platform | S60 5th Edition | Tested on devices | Nokia 5800 XpressMusic |
| Category | Web Runtime (WRT) | Subcategory | PIM |
| Keywords (APIs, classes, methods, functions): device.getServiceObject(), Service.Contact.GetList(), Service.Contact.Add() |
This code snippet shows how to edit contacts in the phone book using the Contact Platform Service for S60 Web Runtime introduced in S60 5th Edition.
To obtain access to the service object for the Contact Service API, the device.getServiceObject("Service.Contact", "IDataSource") method is used.
After setting the correct values for the criteria object (criteria.Type and criteria.Data) and setting criteria.Data.id, the IDataSource.Add(criteria) method is used for updating contact information.
Method IDataSource.GetList(criteria) is used for retrieving existing contacts.
<?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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<script type="text/javascript" src="phonebookedit.js" />
<title></title>
</head>
<body onload="init()">
<input id="searchstring" type="text"></input>
<input type="button" value="Search" onclick="doRefresh();" />
<form id="inputform" action="ddd">
First Name: <input id="firstname" type="text"></input><br />
Last Name: <input id="lastname" type="text"></input><br />
Phone number: <input id="phonenumber" type="text"></input><br />
E-Mail: <input id="email" type="text"></input><br />
<input type="button" value="Update" onclick="doUpdate();" />
</form>
</body>
</html>
// contacts system service object
var phonebook;
// selected item id
var id;
var NO_ITEMS_ERROR_CODE = 1012;
/**
* 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;
}
// setting default id as illegal
id = -1;
}
/**
* 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 searchString = document.getElementById('searchstring').value;
if(searchString != "") {
// if we have some search string add search parameter to criteria
criteria.Filter = new Object();
criteria.Filter.SearchVal = searchString;
}
var contacts = 0;
// 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;
}
if(contacts.ErrorCode != 0) {
// If error code is not "no suitable items", show alert.
if(contacts.ErrorCode != NO_ITEMS_ERROR_CODE) {
alert( contacts.ErrorMessage );
}
// if no items found set id as illegal
id = -1;
return;
} else {
var item = contacts.ReturnValue.getNext();
id = item.id;
var inputForm = document.getElementById("inputform");
if(item.FirstName != undefined) {
inputForm.firstname.value = item.FirstName.Value;
}
if( item.LastName != undefined ) {
inputForm.lastname.value = item.LastName.Value;
}
if( item.MobilePhoneGen != undefined ) {
inputForm.phonenumber.value = item.MobilePhoneGen.Value;
}
if( item.EmailGen != undefined ) {
inputForm.email.value = item.EmailGen.Value;
}
}
}
/**
* function updates information in the addressbook
*/
function doUpdate() {
if (id == -1) {
alert( "Choose an item first" );
return;
}
var criteria = new Object();
criteria.Type = 'Contact';
criteria.Data = new Object();
var inputForm = document.getElementById("inputform");
// demanding first name, but that is not essential
if(inputForm.firstname.value == "") {
alert( "You should input firstname" );
return;
}
// set id of item to update
criteria.Data.id = id;
// adding First Name parameter to new item
criteria.Data.FirstName = new Object();
criteria.Data.FirstName.Value = inputForm.firstname.value;
// adding other parameters if present
if( inputForm.lastname.value != "" ) {
criteria.Data.LastName = new Object();
criteria.Data.LastName.Value = inputForm.lastname.value;
}
if( inputForm.phonenumber.value != "" ) {
criteria.Data.MobilePhoneGen = new Object();
criteria.Data.MobilePhoneGen.Value = inputForm.phonenumber.value;
}
if( inputForm.email.value != "" ) {
criteria.Data.EmailGen = new Object();
criteria.Data.EmailGen.Value = inputForm.email.value;
}
var contacts;
// updating item
try {
contacts = phonebook.IDataSource.Add( criteria );
} catch(err) {
alert( "error updating data" );
return;
}
// looking for error code returned
if( contacts.ErrorCode != 0 ) {
// show alert
alert( contacts.ErrorMessage );
return;
}
alert( "Item updated" );
}
You can view the source file and the executable application in the attached ZIP archive. The archive is available for download at Media:Editing_contacts_in_WRT.zip
No related wiki articles found