Reading and Editing a Contact Item
This article shows how to read and edit a contact item using Symbian C++.
Article Metadata
Platform Security
Article
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


<suggestions from pirosl here have been integrated, and have hence been removed>
Also I think article should mention how contact ids for other contacts than owncard can be obtained
--pirosl 10:58, 15 September 2009 (UTC)