Creating contacts

Here is how you can programmatically create a new phonebook contact. This code example is the universal ExeDll – it can be compiled both for S60 2nd edition and for S60 3rd edition. Note that for 3rd edition the WriteUserData capability is needed to add the contact to the phonebook. If the program didn’t have this capability, the CPbkContactEngine::AddNewContactL would leave with KErrWrite.

From the CreateContact.cpp

CPbkContactEngine* engine = CPbkContactEngine::NewL();
CleanupStack::PushL( engine );

// Create a contact with few default fields
// All the default fields are empty and won't be displayed
// until some information is stored in them
CPbkContactItem* contact = engine->CreateEmptyContactL();
CleanupStack::PushL( contact );

// Set some default fields
contact->FindField( EPbkFieldIdFirstName )->\
TextStorage()->SetTextL( _L("John") );
contact->FindField( EPbkFieldIdLastName )->\
TextStorage()->SetTextL( _L("Smith") );
...
// Store the contact to the phonebook
engine->AddNewContactL( *contact );

CleanupStack::PopAndDestroy( contact );
CleanupStack::PopAndDestroy( engine );

From the CreatContact.mmp

#ifdef EKA2
TARGET CreateContact.exe
TARGETTYPE exe
// Read-write from-to phonebook
CAPABILITY ReadUserData WriteUserData
#else
TARGET CreateContact.dll
TARGETTYPE dll
#endif

This code example was created because of a forum request that I found interesting. If you'd like to see some other feature demonstrated, please, ask, and I'll try to invent something (link works for registered users).

AttachmentSize
CreateContact.zip4.21 KB