Adding contact in Symbian Web Runtime
hamishwillee
(Talk | contribs) m (Hamishwillee - Add Abstract. Tidy wiki text) |
|||
| (20 intermediate revisions by 7 users not shown) | |||
| Line 1: | Line 1: | ||
| − | + | [[Category:Symbian Web Runtime]][[Category:PIM]][[Category:S60 5th Edition]][[Category:Code Examples]] | |
| − | + | {{Abstract|This code example shows how to add contacts to the device's default phone book in Symbian Web Runtime.}} | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | | | + | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | }} | + | |
| + | {{ArticleMetaData <!-- v1.2 --> | ||
| + | |sourcecode= [[Media:Adding contact in WRT.zip]] | ||
| + | |installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) --> | ||
| + | |devices= Nokia 5800 XpressMusic | ||
| + | |sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) --> | ||
| + | |platform= S60 5th Edition | ||
| + | |devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) --> | ||
| + | |dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 --> | ||
| + | |signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer --> | ||
| + | |capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. --> | ||
| + | |keywords= device.getServiceObject(), Service.Contact.Add() | ||
| + | |language= <!-- Language category code for non-English topics - e.g. Lang-Chinese --> | ||
| + | |translated-by= <!-- [[User:XXXX]] --> | ||
| + | |translated-from-title= <!-- Title only --> | ||
| + | |translated-from-id= <!-- Id of translated revision --> | ||
| + | |review-by= <!-- After re-review: [[User:username]] --> | ||
| + | |review-timestamp= <!-- After re-review: YYYYMMDD --> | ||
| + | |update-by= <!-- After significant update: [[User:username]]--> | ||
| + | |update-timestamp= <!-- After significant update: YYYYMMDD --> | ||
| + | |creationdate= 20081210 | ||
| + | |author= [[User:MiGryz]] | ||
| + | <!-- The following are not in current metadata --> | ||
| + | |id= CS001238 | ||
| + | }} | ||
==Overview== | ==Overview== | ||
| − | |||
| − | |||
| − | + | The example uses the Contact Platform Service, introduced in S60 5th Edition. | |
| − | + | ||
| − | + | To obtain access to the service object for the Contact Service API, the {{Icode|device.getServiceObject("Service.Contact", "IDataSource")}} method is used. | |
| − | + | ||
| − | + | ||
| − | + | After setting the correct values for the criteria object ({{Icode|criteria.Type}} and {{Icode|criteria.Data}}), the {{Icode|IDataSource.Add(criteria)}} method is used to add the new contact. | |
| − | + | ||
| − | ==Source | + | ==Source: phonebook.html== |
<code xml> | <code xml> | ||
<?xml version="1.0" encoding="UTF-8"?> | <?xml version="1.0" encoding="UTF-8"?> | ||
| − | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" | + | <!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"> | <html xmlns="http://www.w3.org/1999/xhtml"> | ||
<head> | <head> | ||
| − | <script src="phonebook.js" | + | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> |
| + | <script type="text/javascript" src="phonebook.js" /> | ||
<title></title> | <title></title> | ||
</head> | </head> | ||
<body onload="init()"> | <body onload="init()"> | ||
| − | |||
<div id="inputdata"> | <div id="inputdata"> | ||
| − | + | First Name: <input id="firstname" type="text"></input><br /> | |
| − | First Name: <input id="firstname" type="text"></input | + | Last Name: <input id="lastname" type="text"></input><br /> |
| − | + | Phone number: <input id="phonenumber" type="text"></input><br /> | |
| − | Last Name: <input id="lastname" type="text"></input><br/> | + | E-Mail: <input id="email" type="text"></input><br /> |
| − | Phone number: <input id="phonenumber" type="text"></input><br/> | + | <input type="button" value="Add" onclick="doSave();" /> |
| − | E-Mail: <input id="email" type="text"></input><br/> | + | |
| − | <button | + | |
</div> | </div> | ||
| − | + | </body> | |
| − | + | ||
| − | + | ||
| − | + | ||
</html> | </html> | ||
| − | |||
</code> | </code> | ||
| − | ==Source | + | ==Source: phonebook.js== |
<code javascript> | <code javascript> | ||
// contacts system service object | // contacts system service object | ||
| − | var phonebook; | + | var phonebook; |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
/** | /** | ||
* Setting default data on application load | * Setting default data on application load | ||
*/ | */ | ||
function init() { | function init() { | ||
| − | // | + | // getting contacts system service object |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
try { | try { | ||
phonebook = device.getServiceObject("Service.Contact", "IDataSource"); | phonebook = device.getServiceObject("Service.Contact", "IDataSource"); | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
} catch(err) { | } catch(err) { | ||
| − | alert( " | + | alert( "No Contact service available" ); |
return; | return; | ||
} | } | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
} | } | ||
/** | /** | ||
| − | + | * function adds an item to the default phonebook | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | * function adds item to | + | |
*/ | */ | ||
function doSave() { | function doSave() { | ||
| − | // Criteria object for | + | // Criteria object for adding contacts |
| − | + | ||
var criteria = new Object(); | var criteria = new Object(); | ||
criteria.Type = 'Contact'; | criteria.Type = 'Contact'; | ||
criteria.Data = new Object(); | criteria.Data = new Object(); | ||
| + | |||
// demanding first name, but that is not essential | // demanding first name, but that is not essential | ||
if(document.getElementById('firstname').value == "") { | if(document.getElementById('firstname').value == "") { | ||
| Line 220: | Line 92: | ||
return; | return; | ||
} | } | ||
| − | + | ||
// adding First Name parameter to new item | // adding First Name parameter to new item | ||
criteria.Data.FirstName = new Object(); | criteria.Data.FirstName = new Object(); | ||
| Line 241: | Line 113: | ||
document.getElementById('email').value; | document.getElementById('email').value; | ||
} | } | ||
| − | + | ||
| − | var | + | var result = 0; |
try { | try { | ||
| − | + | result = phonebook.IDataSource.Add(criteria); | |
} catch(err) { | } catch(err) { | ||
alert( "error saving data" ); | alert( "error saving data" ); | ||
return; | return; | ||
} | } | ||
| − | + | if(result.ErrorCode != 0) { | |
| − | if( | + | alert(result.ErrorMessage); |
| − | + | ||
| − | alert( | + | |
} | } | ||
| − | |||
| − | |||
} | } | ||
| − | |||
</code> | </code> | ||
| − | |||
==Postconditions== | ==Postconditions== | ||
| − | |||
| − | |||
| − | + | * Filling in the fields and selecting "Add" will add a new item to the default phone book. | |
| − | + | ||
| − | + | ==Supplementary material== | |
| − | + | ||
| − | + | You can view the source file and executable application in the attached ZIP archive. The archive is available for download at [[Media:Adding contact in WRT.zip]]. | |
| − | You can | + | |
==See also== | ==See also== | ||
| − | |||
| − | [[ | + | * [[Retrieving contacts info in Symbian Web Runtime]] |
| + | * [[Removing contacts in Symbian Web Runtime]] | ||
Latest revision as of 09:59, 5 October 2012
This code example shows how to add contacts to the device's default phone book in Symbian Web Runtime.
Article Metadata
Code Example
Source file: Media:Adding contact in WRT.zip
Tested with
Devices(s): Nokia 5800 XpressMusic
Compatibility
Platform(s): S60 5th Edition
Article
Keywords: device.getServiceObject(), Service.Contact.Add()
Created: MiGryz
(10 Dec 2008)
Last edited: hamishwillee
(05 Oct 2012)
Contents |
Overview
The example uses the Contact Platform Service, 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), the IDataSource.Add(criteria) method is used to add the new contact.
Source: 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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<script type="text/javascript" src="phonebook.js" />
<title></title>
</head>
<body onload="init()">
<div id="inputdata">
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="Add" onclick="doSave();" />
</div>
</body>
</html>
Source: phonebook.js
// contacts system service object
var phonebook;
/**
* 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;
}
}
/**
* function adds an item to the default phonebook
*/
function doSave() {
// Criteria object for adding contacts
var criteria = new Object();
criteria.Type = 'Contact';
criteria.Data = new Object();
// demanding first name, but that is not essential
if(document.getElementById('firstname').value == "") {
alert("You should input firstname");
return;
}
// adding First Name parameter to new item
criteria.Data.FirstName = new Object();
criteria.Data.FirstName.Value =
document.getElementById('firstname').value;
// adding other parameters if present
if(document.getElementById('lastname').value != "") {
criteria.Data.LastName = new Object();
criteria.Data.LastName.Value =
document.getElementById('lastname').value;
}
if(document.getElementById('phonenumber').value != "") {
criteria.Data.MobilePhoneGen = new Object();
criteria.Data.MobilePhoneGen.Value =
document.getElementById('phonenumber').value;
}
if(document.getElementById('email').value != "") {
criteria.Data.EmailGen = new Object();
criteria.Data.EmailGen.Value =
document.getElementById('email').value;
}
var result = 0;
try {
result = phonebook.IDataSource.Add(criteria);
} catch(err) {
alert( "error saving data" );
return;
}
if(result.ErrorCode != 0) {
alert(result.ErrorMessage);
}
}
Postconditions
- Filling in the fields and selecting "Add" will add a new item to the default phone book.
Supplementary material
You can view the source file and executable application in the attached ZIP archive. The archive is available for download at Media:Adding contact in WRT.zip.

