Manage Contacts with Exchange Web Services (EWS)
Aspose.Email lets you work with more than just email messages on Microsoft Exchange Server. Using the EWSClient API, you can retrieve, resolve, create, update, and delete contacts stored in an Exchange mailbox. This article explains the key operations for managing contacts with Exchange Web Services (EWS).
The client represents a contact as a Contact object. The samples in this article use these headers and namespaces:
#include <ObjectIdentifier.h>
#include <PersonalInfo/Contact.h>
#include <PersonalInfo/Gender.h>
#include <PersonalInfo/TextFormat.h>
#include <PersonalInfo/EmailAddress.h>
#include <PersonalInfo/EmailAddressList.h>
#include <PersonalInfo/EmailAddressCategory.h>
#include <PersonalInfo/PhoneNumber.h>
#include <PersonalInfo/PhoneNumberList.h>
#include <PersonalInfo/PhoneNumberCategory.h>
#include <PersonalInfo/AssociatedPerson.h>
#include <PersonalInfo/AssociatedPersonsList.h>
#include <PersonalInfo/AssociatedPersonCategory.h>
#include <PersonalInfo/Url.h>
#include <PersonalInfo/URLList.h>
#include <PersonalInfo/UrlCategory.h>
#include <Clients/Exchange/WebService/EWSClient/EWSClient.h>
#include <Clients/Exchange/WebService/EWSClient/IEWSClient.h>
#include <Clients/Exchange/WebService/EWSClient/DeletionOptions.h>
#include <Clients/Exchange/WebService/ExchangeListContactsOptions.h>
#include <Clients/Exchange/ExchangeMailboxInfo.h>
#include <system/array.h>
#include <system/convert.h>
using namespace System;
using namespace Aspose::Email;
using namespace Aspose::Email::PersonalInfo;
using namespace Aspose::Email::Clients::Exchange;
using namespace Aspose::Email::Clients::Exchange::WebService;
Get Contacts with EWS
GetContacts reads all contacts of a folder; the URI of the default Contacts folder comes from the mailbox information. The example below shows each contact’s name and first e-mail address.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
ArrayPtr<SharedPtr<Contact>> contacts = client->GetContacts(client->get_MailboxInfo()->get_ContactsUri());
for (SharedPtr<Contact> contact : contacts)
{
SharedPtr<EmailAddress> email = contact->get_EmailAddresses()->get_Email1();
Console::WriteLine(u"Name: " + contact->get_DisplayName() + u", Email Address: " + (email != nullptr ? email->ToString() : String(u"-")));
}
client->Dispose();
Resolve Contacts by Name
ResolveContacts searches the contacts and the directory for entries that match a name, the same way Outlook resolves a name typed into the To field. ExchangeListContactsOptions::FetchPhoto also downloads the contact photos.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
ArrayPtr<SharedPtr<Contact>> contacts = client->ResolveContacts(u"Changed Name", ExchangeListContactsOptions::FetchPhoto);
for (SharedPtr<Contact> contact : contacts)
{
Console::WriteLine(u"Name: " + contact->get_DisplayName());
}
client->Dispose();
Determine Contact Notes Format
The Contact->get_NotesFormat property specifies the format of the notes text according to the TextFormat enumerator.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
for (SharedPtr<Contact> contact : client->GetContacts(client->get_MailboxInfo()->get_ContactsUri()))
{
Console::WriteLine(contact->get_DisplayName() + String(u": notes are ") + (contact->get_NotesFormat() == TextFormat::Html ? u"HTML" : u"plain text"));
}
client->Dispose();
Fetch Contact using Id
Every contact stored on the server has an ID - get_Id returns it, and CreateContact returns the ID of a new one. GetContact reads a single contact by that ID.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
ArrayPtr<SharedPtr<Contact>> contacts = client->GetContacts(client->get_MailboxInfo()->get_ContactsUri());
String id = contacts[0]->get_Id()->get_EWSId();
SharedPtr<Contact> fetchedContact = client->GetContact(id);
Console::WriteLine(u"Fetched: " + fetchedContact->get_DisplayName());
client->Dispose();
Add Contacts
Use the CreateContact() method to add a new contact to Exchange Server. Phone numbers, related persons, web addresses and e-mail addresses are kept in lists on the contact; each entry has a category - home phone, spouse, blog, first e-mail address and so on - taken from the matching ...Category class.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
SharedPtr<Contact> contact = MakeObject<Contact>();
contact->set_Gender(Gender::Male);
contact->set_DisplayName(u"Frank Lin");
contact->set_CompanyName(u"ABC Co.");
contact->set_JobTitle(u"Executive Manager");
SharedPtr<PhoneNumber> phone = MakeObject<PhoneNumber>();
phone->set_Number(u"123456789");
phone->set_Category(PhoneNumberCategory::get_Home());
contact->get_PhoneNumbers()->Add(phone);
SharedPtr<AssociatedPerson> spouse = MakeObject<AssociatedPerson>();
spouse->set_Name(u"Catherine");
spouse->set_Category(AssociatedPersonCategory::get_Spouse());
contact->get_AssociatedPersons()->Add(spouse);
SharedPtr<Url> blog = MakeObject<Url>();
blog->set_Href(u"www.blog.com");
blog->set_Category(UrlCategory::get_Blog());
contact->get_Urls()->Add(blog);
SharedPtr<EmailAddress> email = MakeObject<EmailAddress>(u"Frank.Lin@Abc.com", String(u"Frank Lin"));
email->set_Category(EmailAddressCategory::get_Email1());
contact->get_EmailAddresses()->Add(email);
try
{
String id = client->CreateContact(contact);
Console::WriteLine(u"Contact created: " + id);
}
catch (System::Exception& ex)
{
Console::WriteLine(ex->get_Message());
}
client->Dispose();
Update Contacts
Contact information can be modified on the server using IEWSClient->UpdateContact: change the properties of a contact read from the server and pass it back. The following code sample renames the first contact of the Contacts folder.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
ArrayPtr<SharedPtr<Contact>> contacts = client->GetContacts(client->get_MailboxInfo()->get_ContactsUri());
SharedPtr<Contact> contact = contacts[0];
Console::WriteLine(u"Name: " + contact->get_DisplayName());
contact->set_DisplayName(u"David Ch");
client->UpdateContact(contact);
client->Dispose();
Deleting Contacts
A contact is deleted like any other item of the mailbox: DeleteItem takes its ID and the DeletionOptions - get_DeletePermanently() removes the item, get_MoveToDeletedItems() moves it to the Deleted Items folder.
The following code snippet deletes the contacts with a given display name.
SharedPtr<IEWSClient> client = EWSClient::GetEWSClient(u"https://outlook.office365.com/ews/exchange.asmx", u"username", u"password");
String contactToDelete = u"John Teddy";
for (SharedPtr<Contact> contact : client->GetContacts(client->get_MailboxInfo()->get_ContactsUri()))
{
if (contact->get_DisplayName() == contactToDelete)
{
client->DeleteItem(contact->get_Id()->get_EWSId(), DeletionOptions::get_DeletePermanently());
}
}
client->Dispose();