Synchronizing emails with MS Exchange

Advanced

Synchronization with various services of MS Exchange via EWS protocol (Exchange Web Services) is supported by the Sync Engine synchronization mechanism. This article describes the synchronization of email in Creatio with MS Exchange. Email in Creatio is synchronized only from MS Exchange. Since emails can no longer be modified after they have been sent, only the emails that have not been synchronized previously are synchronized. The main difference between the email synchronization mechanism and integration is the email search engine in Creatio. Since the same email can be synchronized on behalf of any of the recipients or even via IMAP protocol, metadata synchronization can not be used for searching for previously synchronized emails. Use subject, send date and message to search for emails . All markups and spaces are removed from the message. To speed up the search, use the md5 hash that is stored in the MailHash column of the Activity object.

The second difference of this synchronization is that attachments are synchronized by a separate process, after all emails are processed. This is done in order to make the attachment download time not affect the email save time.

Integration implementation 

As described in the "Sync Engine synchronization mechanism basics " article, to implement integration using this mechanism, a class is required that implements the logic of working with external storage (RemoteProvider heir) and a class that implements the IRemoteItem interface, which is an instance of the synchronization element (in our case — email MS Exchange).

scr_syncengine_msexchangeemail_hierarchy_sheme_remoteprovider_1.png

The ExchangeEmailSyncProvider class is the service provider for the exchange external storage. This class implements the logic of selecting data from MS Exchange.

The ExchangeEmailMessage class implements the IRemoteItem interface, in which the logic of filling in data in Creatio objects is implemented.

The ExchangeUtility class contains methods for the EWS API library and the methods used to download email attachments.

The ExchangeEmailMessageUtility class contains methods for converting the lookup values of the email fields.

scr_syncengine_msexchangeemail_hierarchy_sheme_remoteitem_1.png

Synchronized data 

The correspondence of Creatio objects to the EmailMessage class fields is shown on table.

The correspondence of Creatio objects to the EmailMessage class fields

Creatio object Object field EmailMessage corresponding field
Activity Title Subject
  Body Body.Text
  Sender Sender
  Recepient ToRecepients
  CopyRecepient CcRecepients
  BlindCopyRecepient BccRecepients
  SendDate DateTimeSpent
  Priority <Importance
  DueDate, StartDate DateTimeReceived
ActivityFile Name Name
  Data Content
  Size Content.Length

Logic of filling in email participants 

For an email to be displayed correctly only for users who have synchronized it, the following mechanism of filling in the Activity participants detail has been implemented. Conventionally, this logic can be divided into two parts:

  1. Adding participants to a new email.
  2. Updating the list of participants when an email changes (including re-synchronization).

Adding participants to a new email 

The main value that affects who becomes the participants of an email is the Communication contact detail. If a contact has an email address in the Communication detail, and this email is listed in one of the email address fields (From, To, CC, BCC), the contact can be added to the participants. Additionally, a check is made whether there is a system user for this contact who is not a portal user. A user is added to the participants only after they have synchronized their email.

Updating the list of participants 

For a user to become a participant after the synchronization of an existing email, the list of participants is updated - all participants who are not Creatio users are removed from the detail, and the algorithm of filling in detail for a new email runs. Thus, the users who have previously synchronized the email remain as participants, a new user is added, and the contact list is updated.

Logic of selecting data for synchronization 

When selecting emails for synchronization with MS Exchange, use the following filter set: select emails that have been modified since the last synchronization and are not drafts. There is a limit for synchronization folders: "Deleted" and "Conflicting elements" folders do not participate in synchronization. When selecting emails the synchronization metadata is not taken into account. Always "save changes in Creatio". When processing each email, the system first checks for emails in Creatio. If an email already exists in Creatio, the participants are updated. If not, a new email is created. At the end of the synchronization, the system adds a task to synchronize attachments.