Search code examples
outlookvstooutlook-addinoffice-interop

Office interop is unable to get the updated TaskItem from outlook when task is updated from Outlook Web/Office Online


I am developing an outlook add in that retrieves a TaskItem from outlook desktop and opens it. Here is my code.

private static Microsoft.Office.Interop.Outlook.TaskItem GetLatestTask(string entryId)
 {
    Microsoft.Office.Interop.Outlook.MAPIFolder taskFolder = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderTasks);
    List<Microsoft.Office.Interop.Outlook.TaskItem> liTask = new List<Microsoft.Office.Interop.Outlook.TaskItem>();
    foreach (Microsoft.Office.Interop.Outlook.TaskItem taskItem in taskFolder.Items)
    {
        if (taskItem.EntryID == entryId)
            return taskItem;
    }
}

This works fine usually. However if i update the task from outlook web/Office online, then try to get the task using the code, the task i get is not updated and still contains the old values.

So for example i have a task with a subject named "Test" then i update this in outlook web to "Test Updated", I would still get a task with a subject named "Test".

If I check the task list in outlook desktop, I can see that the task's subject has already been updated in the Task list. However opening it would still display the old item.

Once I restart Outlook, The add in gets the updated item.

Can anyone point me in the right direction? Thank you.


Solution

  • First of all, iterating over all items in the folder is not really a good idea:

    foreach (Microsoft.Office.Interop.Outlook.TaskItem taskItem in taskFolder.Items)
        {
            if (taskItem.EntryID == entryId)
                return taskItem;
        }
    

    Instead, you may consider using the Find/FindNext or Restrict methods of the Items class. Read more about them in the following articles with code sample:

    Also relying on the EntryID property for identifying items in Outlook is not the best way. The Entry ID changes when an item is moved into another store, for example, from your Inbox to a Microsoft Exchange Server public folder, or from one Personal Folders (.pst) file to another .pst file. Solutions should not depend on the EntryID property to be unique unless items will not be moved. Instead, you may introduce your own custom property for identifying items.

    If you use cached data most probably you need to sync your data with the server side. SyncObjects is a set of SyncObject objects representing the Send/Receive groups for a user. Use the SyncObjects property to return the SyncObjects object from a NameSpace object.

    Set mySyncObjects = Application.GetNameSpace("MAPI").SyncObjects
    

    The SyncObject.Start method begins synchronizing a user's folders using the specified Send\Receive group.

    Public Sub Sync() 
     Dim nsp As Outlook.NameSpace 
     Dim sycs As Outlook.SyncObjects 
     Dim syc As Outlook.SyncObject 
     Dim i As Integer 
     Dim strPrompt As Integer 
     Set nsp = Application.GetNamespace("MAPI") 
     Set sycs = nsp.SyncObjects 
     For i = 1 To sycs.Count 
    Set syc = sycs.Item(i) 
    strPrompt = MsgBox( _ 
     "Do you wish to synchronize " & syc.Name &"?", vbYesNo) 
    If strPrompt = vbYes Then 
     syc.Start 
    End If 
     Next 
    End Sub
    

    Hopefully, after that, your items will be updated with a new data.