A way of detecting if an Outlook Contact has been updated since last synced with the server

Standard

This entry concerns a common problem in the world of occasionally connected clients and synchronising changes.  I thought I’d document my solution as it uses less known Outlook attributes.  Those doing similar things might find it useful.   It assumes a knowledge of VSTO, Visual Studio 2005 and VB.NET. 

At the moment I’m working on a process to synchronise Outlook contacts items with contacts stored in a database on the Server.

There’s always a question of which updates take priority (Outlook or Server?), and how to detect if things are updated.

The Rules

I’ve devised a couple of rules that determine which updates are the master:

  • If the Server record has been updated since we last synced, overwrite the local record
  • If the local record has been modified since the last server update, don’t overwrite it

Implementation

When translating a server record to a client contact, I save the following properties to the Outlook Contact:

loNewContact.SetUserProperty("ServerContactId", ServerRecord.Id)
loNewContact.SetUserProperty("ServerTimestamp", ServerRecord.Vers)
loNewContact.SetUserProperty("ServerLastUpdateDateTime", Now.AddSeconds(20))

loNewContact.Save

 

ServerContactId : Associates the client record with the server record

ServerTimestamp: Is the value of a field used on the server to indicate when the server record is updated.  In this case it’s an integer. 

ServerLastUpdateDateTime : This indicates when this record is synced.  It is compared with Outlook’s Local LastModificationTime property in order to determine if the local record has been updated since last sync.  The adding of 20 seconds to the ServerLastUpdate property is because calling “Save” on the contact will update the LastModificationTime property to a later value.

 

The “SetUserProperty” routine is a quick way of setting user properties. ie:  

Public Sub SetUserProperty(ByVal name As String, ByVal value As String)  

       With CType(moOutlookItem.UserProperties, UserProperties)
           Dim loProp As UserProperty = .Find(name)
           If loProp Is Nothing Then
               loProp = .Add(name, OlUserPropertyType.olText)
           End If
           loProp.Value = value
       End With
End Sub

 

The synchronisation logic contains a loop that goes through the server items, checking if they are already local (using the ServerContactId user property.

For items that already exist, I use this piece of code to determine whether to update or not:

 

If mbTheServerRecordHasBeenModifiedSinceLastUpdate(loServerContact, loPimContact) Then

       ' Update the local record
        ....

Else
      ' If the contact has been updated since the last server sync
      If mbThePimContactHasBeenUpdated(loPimContact) Then

                      ' Issue an update request to the server
                    

       End If
End If

 

The routines look like this:

 

Private Function mbTheServerRecordHasBeenModifiedSinceLastUpdate(ByVal serverContact As ContactDetail, ByVal pimContact As IPimContactItem) As Boolean
    Dim lsServerVers As String = pimContact.GetUserProperty("ServerTimestamp")
    If IsNumeric(lsServerVers) Then

        ' If the server has been updated since the last update
        ' of this record
        Return (CInt(lsServerVers) <> serverContact.Vers)

    End If
 
    Return False
End Function

Private Function mbThePimContactHasBeenUpdated(ByVal pimContact As IPimContactItem) As Boolean
    Dim lsLastServerUpdateDateTime As String = pimContact.GetUserProperty("ServerLastUpdateDateTime")
    Dim loPimContactLastModificationDateTime As Date = pimContact.LastModificationDateTime
    If IsDate(lsLastServerUpdateDateTime) Then

        Return CDate(lsLastServerUpdateDateTime) < loPimContactLastModificationDateTime

    End If
 
    Return False
End Function

 

I’m sorry to say I do not have a downloadable code sample you can use, as it tends to be quite specific to the associated server process.    If you have any interest or comments about this code, please post a comment.

Advertisements

3 responses »

  1. hello, anoriginalidea
    Its really a nice article.
    I am seaching for synchronization betwwen Sql Server 2005 and outlook 2007. Pls let me know how can I synchronize my Outlook Inbox folder with sql server.
    Regards
    Ctal

  2. You need to create a table in the database which has fields similar to those of the mail items in your inbox, then you need to write an Outlook Addin to iterate through your inbox, updating the table.

    Using techniques like the one described above will help you “sync” rather than just “extract”.

  3. Hi anoriginalidea
    At last i found a great article regrading to my issue.
    I have one more issue, i am using own application, which adds a new contact into outlook. it is working fine with one bug 😦
    when newly added contact is opened in outlook, and without changing anything contact is being closed it raises an alert
    “Do you want to save? Yes No Cancel” i am not sure why it is happening.
    Intersting point is that, it happens first time only, and when alert appears, just press anything(Yes/NO) it will close, and from second onwards, it will not rasie alert.
    Can you suggest me, which section or value is responsible for outlook to raise the alert. I am trying to find out outlook contact details with software OutLookspy.
    Hope this help you to understand my problem.

    Thanks
    Alok

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s