Retrieve, Manage, and Update Contacts in Exchange Server Using EWS

Get Contacts with EWS

Aspose.Email provides the EWSClient class to connect to Microsoft Exchange Server using Exchange Web Services. The code snippets that follow use Exchange Web Services to read all the contacts. The following code snippet shows you how to get Contacts with EWS.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Create instance of IEWSClient class by giving credentials
IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
// List all the contacts
Contact[] contacts = client.GetContacts(client.MailboxInfo.ContactsUri);
foreach (MapiContact contact in contacts)
{
// Display name and email address
Console.WriteLine("Name: " + contact.NameInfo.DisplayName + ", Email Address: " + contact.ElectronicAddresses.Email1);
}

Resolve Contacts by Name

The following code snippet shows you how to use get contacts with EWS

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Create instance of IEWSClient class by giving credentials
IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
// List all the contacts
Contact[] contacts = client.ResolveContacts("Changed Name", ExchangeListContactsOptions.FetchPhoto);
foreach (MapiContact contact in contacts)
{
// Display name and email address
Console.WriteLine("Name: " + contact.NameInfo.DisplayName + ", Email Address: " + contact.ElectronicAddresses.Email1);
}

Determine Contact Notes Format

The Aspose.Email.Mail.Contact.NotesFormat specifies the contacts notes text format type defined by Aspose.Email.TextFormat enumerator.

Fetch Contacts by ID

A particular contact can be retrieved from the server using its contact id as shown in the following code sample.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
Contact fetchedContact = client.GetContact(id);

Adding Contacts

The EWSClient class CreateContact() method can be used to add Contact information to an Exchange Server. The CreateContact() method takes a Contact object as an input parameter.

To add contacts to an Exchange Server:

  1. Initialize the EWSClient with address and credentials.
  2. Initialize the Contact object with the desired properties.
  3. Call the CreateContact method to add the contact to the Exchange Server.

Aspose.Email provides the EWSClient class to connect to Microsoft Exchange Server using Exchange Web Services. The code snippets show you how to use Exchange Web Services to add contacts to an Exchange Server.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Set mailboxURI, Username, password, domain information
string mailboxUri = "https://ex2010/ews/exchange.asmx";
string username = "test.exchange";
string password = "pwd";
string domain = "ex2010.local";
NetworkCredential credentials = new NetworkCredential(username, password, domain);
IEWSClient client = EWSClient.GetEWSClient(mailboxUri, credentials);
//Create New Contact
Contact contact = new Contact();
//Set general info
contact.Gender = Gender.Male;
contact.DisplayName = "Frank Lin";
contact.CompanyName = "ABC Co.";
contact.JobTitle = "Executive Manager";
//Add Phone numbers
contact.PhoneNumbers.Add(new PhoneNumber { Number = "123456789", Category = PhoneNumberCategory.Home });
//contact's associated persons
contact.AssociatedPersons.Add(new AssociatedPerson { Name = "Catherine", Category = AssociatedPersonCategory.Spouse });
contact.AssociatedPersons.Add(new AssociatedPerson { Name = "Bob", Category = AssociatedPersonCategory.Child });
contact.AssociatedPersons.Add(new AssociatedPerson { Name = "Merry", Category = AssociatedPersonCategory.Sister });
//URLs
contact.Urls.Add(new Url { Href = "www.blog.com", Category = UrlCategory.Blog });
contact.Urls.Add(new Url { Href = "www.homepage.com", Category = UrlCategory.HomePage });
//Set contact's Email address
contact.EmailAddresses.Add(new EmailAddress { Address = "Frank.Lin@Abc.com", DisplayName = "Frank Lin", Category = EmailAddressCategory.Email1 });
try
{
client.CreateContact(contact);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

Updating Contacts

Contact information can be updated using Microsoft Outlook. Aspose.Email can also update contact information on Exchange Server using the Exchange Web Service (EWS). The IEWSClient UpdateContact method can update contact information on Exchange Server.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
IEWSClient client = EWSClient.GetEWSClient(mailboxUri, credentials);
// List all the contacts and Loop through all contacts
Contact[] contacts = client.GetContacts(client.MailboxInfo.ContactsUri);
Contact contact = contacts[0];
Console.WriteLine("Name: " + contact.DisplayName);
contact.DisplayName = "David Ch";
client.UpdateContact(contact);

Deleting Contacts

The EWSClient class allows the DeleteContact to access and delete contacts from an Exchange Server contacts folder. This method takes the contact ID or MapiContact as an input parameter.

To delete contacts from an Exchange Server:

  1. Initialize the ExchangeWebServiceClient with address and credentials.
  2. Delete a contact using its ID.
  3. Delete a contact by calling the DeleteContact method with MapiContact as input parameter.

The following code snippets show you how to delete contacts from an exchange server using exchange web service.

// For complete examples and data files, please go to https://github.com/aspose-email/Aspose.Email-for-.NET
// Create instance of EWSClient class by giving credentials
IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");
string strContactToDelete = "John Teddy";
Contact[] contacts = client.GetContacts(client.MailboxInfo.ContactsUri);
foreach (Contact contact in contacts)
{
if (contact.DisplayName.Equals(strContactToDelete))
client.DeleteItem(contact.Id.EWSId, DeletionOptions.DeletePermanently);
}
client.Dispose();

Working with Extended Contact Properties


 IEWSClient client = EWSClient.GetEWSClient("https://outlook.office365.com/ews/exchange.asmx", "testUser", "pwd", "domain");

//The required extended properties must be added in order to create or read them from the Exchange Server

string[] extraFields = new string[] {{ "User Field 1", "User Field 2", "User Field 3", "User Field 4" }};

foreach (string extraField in extraFields)

    client.ContactExtendedPropertiesDefinition.Add(extraField);

//Create a test contact on the Exchange Server

Contact contact = new Contact();

contact.DisplayName = "EMAILNET-38433 - " + Guid.NewGuid().ToString();

foreach (string extraField in extraFields)

    contact.ExtendedProperties.Add(extraField, extraField);

string contactId = client.CreateContact(contact);

//retrieve the contact back from the server after some time

Thread.Sleep(5000);

contact = client.GetContact(contactId);

//Parse the extended properties of contact

foreach (string extraField in extraFields)

    if (contact.ExtendedProperties.ContainsKey(extraField))

        Console.WriteLine(contact.ExtendedProperties[extraField].ToString());