×
Namespaces

Variants
Actions

Reading and Editing a Contact Item

From Nokia Developer Wiki
Jump to: navigation, search

This article shows how to read and edit a contact item using Symbian C++.

Article Metadata
Platform Security
Capabilities: WriteUserData
Article
Keywords: CContactDatabase
Created: vvsnaresh (21 Mar 2007)
Last edited: vineet.jain (15 Feb 2012)

Opening a contact for reading

Whenever a contact item is opened for editing, it is locked to prevent simultaneous edits from other clients of the Contacts Model and it remains locked until it is either committed or closed. Therefore, unless you wish to edit a contact item, the item should be opened as read only with the CContactDatabase::ReadContactL() or ReadContactLC() function.

The following code shows how to read information from a contact, in this case the "own card" contact:

// Open the default contacts database:
CContactDatabase* contactsDb = CContactDatabase::OpenL();
CleanupStack::PushL(contactsDb);
 
// Get the ID of the own card and open the contact:
TContactItemId ownCardId = contactsDb->OwnCardId();
if (ownCardId == KNullContactId ) User::Leave(KNullContactId ); //Own card may not be set
CContactItem* ownCard = contactsDb->ReadContactL(ownCardId);
CleanupStack::PushL(ownCard);
 
TInt count = ownCard->CardFields().Count();
contactsDb->CloseContactL(ownCard->Id());
CleanupStack::PopAndDestroy(2); // ownCard, contactsDb

Opening a contact for editing

The following code example shows how to open a contact for editing. The fields within a CContactItemFieldSet are indexed and the index of a particular field may be obtained by calling the CContactItemFieldSet::Find() function with the UID of the field that you are searching for. In the following example, the index that represents the forename field of the contact is obtained and this is subsequently used to modify the text in the field. Once editing the contact has been completed, the changes can be committed to the database by calling the CContactDatabase::CommitContactL() function. The CContactDatabase::CloseContactL() function closes the contact without saving the changes.

ownCard = contactsDb->OpenContactL(ownCardId);
CleanupStack::PushL(ownCard);
 
TInt index = ownCard->CardFields().Find(KUidContactFieldGivenName);
 
if(index != KErrNotfound) {
ownCard->CardFields()[index].TextStorage()->SetTextL(KOtherForename);
}
contactsDb->CommitContactL(*ownCard);
 
CleanupStack::PopAndDestroy(2); // ownCard contactsDb
This page was last modified on 15 February 2012, at 12:52.
59 page views in the last 30 days.
×