×
Namespaces

Variants
Actions
(Difference between revisions)

Adding contacts to a group in Symbian Web Runtime

From Nokia Developer Wiki
Jump to: navigation, search
ivruban (Talk | contribs)
croozeus (Talk | contribs)

Revision as of 13:46, 26 February 2009


Article Metadata
Tested with
Devices(s): Nokia 5800 XM
Compatibility
Platform(s): S60 5th Edition
S60 5th Edition
Article
Keywords: device.getServiceObject(), Service.IDataSource.GetList(), Service.IDataSource.Organise()
Created: (28 Jan 2009)
Last edited: croozeus (26 Feb 2009)


Overview

This code snippet shows how to use methods of Contacts Service API to add contact to the contact group using widget.

To obtain access to service object for the Contacts Service API, method getServiceObject("Service.Contact", "IDataSource") used.

While initialization application load list of contacts and contacts groups using function Service.IDataSource.GetList(). criteria object used to determine the type of data. After choosing the contact and contact group Service.IDataSource.Organise() function used to add the contact to contact group. To setup a correct values for Service.IDataSource.Organise() function criteria object used.

criteria object contains a criteria.Type, criteria.Data and criteria.OperationType to define the organize operation.

groupdata object used to define the contact group and contacts for criteria.Data object.

Source file: Relevant HTML components

<select id = "contacts" size = "5">
<option></option>
</select><br/>
<select id = "groups" size = "5">
<option></option>
</select><br/>
<input type = "button" onclick = "doAdd();"
value = "Add to group"/>

Source file: JavaScript file

window.onload = init;
 
// Declare the service object
var so;
/**
* Java Script function
* Fill the list of contacts
* @param iterator List of contacts, equals result.ReturnValue of
* Service.IDataSource.GetList() function return value
*/

function fillContactsList(iterator){
var contactList = document.getElementById("contacts");
try{
iterator.reset();
var item;
while (( item = iterator.getNext()) != undefined ){
// Last name of contact
var lastName = item.LastName.Value;
// First name of contact
var firstName = item.FirstName.Value;
// Contact id
var id = item.id;
var node = document.createElement("option");
node.value = id;
node.appendChild (document.createTextNode(lastName + " " +
firstName));
contactList.appendChild(node);
}
} catch (e) {
alert("fillContactsList" + e);
}
}
/**
* Java Script function
* Fill the list of groups
* @param iterator List of groups, equals result.ReturnValue of
* Service.IDataSource.GetList() function return value
*/

function fillGroupsList(iterator){
var groupList = document.getElementById("groups");
try{
iterator.reset();
var item;
while (( item = iterator.getNext()) != undefined ){
// Name of group
var groupName = item.GroupLabel;
// Group's id
var id = item.id;
var node = document.createElement ("option");
node.value = id;
node.appendChild (document.createTextNode(groupName));
groupList.appendChild(node);
}
} catch (e) {
alert("fillContactsList" + e);
}
}
 
/**
* Java Script function
* Get the list of contacts
*/

function getContactList() {
// Criteria for Service.IDataSource.GetList() function
var criteria = new Object();
// Type of list
criteria.Type = "Contact";
try {
var result;
// Service.IDataSource.GetList() function - method retrieves a list of
//contacts
result = so.IDataSource.GetList(criteria);
if( result.ErrorCode == 0) {
//Fill the list of contacts
fillContactsList(result.ReturnValue);
} else {
alert("Error in getting contact list");
}
} catch (e) {
alert ("getContactList: " + e);
}
}
/**
* Java Script function
* Get the list of contacts
*/

function getGroupsList() {
// Criteria for Service.IDataSource.GetList() function
var criteria = new Object();
// Type of list
criteria.Type = "Group";
try {
var result;
// Service.IDataSource.GetList() function - method retrieves a list of
//contact groups
result = so.IDataSource.GetList(criteria);
if(result.ErrorCode == 0) {
//Fill the list of contact groups
fillGroupsList(result.ReturnValue);
} else {
alert("Error in getting contact list");
}
} catch (e) {
alert ("getContactList: " + e);
}
}
 
// Initializes the widget
function init() {
try {
//Get the service object
so = device.getServiceObject("Service.Contact", "IDataSource");
} catch (e) {
alert("init: " + e );
}
// Clean the contact list
var contactList = document.getElementById("contacts");
while(contactList.length != 0){
contactList.remove(0);
}
// Clean the contact group list
var groupList = document.getElementById("groups");
while(groupList.length != 0){
groupList.remove(0);
}
// Get the list of contacts
getContactList();
// Get the list of contact groups
getGroupsList();
// Select the first contact group
groupList.options[0].selected = true;
// Select the first contact
contactList.options[0].selected = true;
}
// Add selected contact to the selected contact group
function doAdd(){
var contact;
var group;
// Get the selected contact group
var groupList = document.getElementById("groups");
for(var i = 0; i < groupList.options.length; i++) {
if (groupList.options[i].selected) {
group = groupList.options[i];
}
}
// Get the selected contact
var contactList = document.getElementById("contacts");
for(var i = 0; i < contactList.options.length; i++) {
if (contactList.options[i].selected) {
contact = contactList.options[i];
}
}
// Create the contact and contact group data to update
var groupdata = new Object();
// The id of selected contact group
groupdata.id = group.value;
groupdata.IdList = new Array();
// The id of selected contact
groupdata.IdList[0] = contact.value;
// Criteria for update contact and contact group
var criteria = new Object();
criteria.Type = "Group";
criteria.Data = groupdata;
// Add the contact to group, "Disassociate" to delete
criteria.OperationType = "Associate";
 
try {
// Organise method adds contacts to a contact group (association)
var result = so.IDataSource.Organise(criteria);
if(result.ErrorCode == 0) {
alert("Contact added to group successfully");
} else {
alert("Error occurs while organizing contacts");
}
} catch (e) {
alert("Exception occure while organizing contacts: " + e);
}
}

Postconditions

After starting of application, the "Contacts" and "Contact Groups" listboxes are shown.
To associate contact to the contact group use "Add to group" button.

Supplementary material

  • You can test the adding of contact to contact group in action in a simple, executable application into which this code snippet has been patched. The executables and source files are available for download at: Media:AddingContactsToGroup.zip.
  • You can examine all the changes that are required to implement the above mentioned features in an application. The changes are provided in unified diff and color-coded diff formats: link_to_patchs
173 page views in the last 30 days.
×