AdvancedSearch never fires AdvancedSearchComplete event

Add-in Express™ Support Service
That's what is more important than anything else

AdvancedSearch never fires AdvancedSearchComplete event
 
tadams


Guest


Hi,

I am having some strange behaviour regarding one user where we can see that the AdvancedSearch is started but the complete event never fires. There are no errors being thrown that we can see either.

We have already tried scoping the search period to 1 day and changing the date it is seaching, however this doesn't help things. The weird thing is that this is working fine for most other users across multiple versions of Outlook, so there must be some edge case relating to this user's particular instance...

Do you know any reasons why AdvancedSearchComplete event might not ever trigger?

Thanks!
Tom

Here is the relevant code:


    Public emailSearch As Outlook.Search = Nothing
    Public syncInProcess As Boolean = False


    Sub doAdvancedSearch(ByVal syncFolderPathStr As String, ByVal syncFilterStr As String)
        Try
            syncInProcess = True
            emailSearch = olApp.AdvancedSearch(syncFolderPathStr, syncFilterStr, True, "getEmails")
        Catch ex As Exception
            syncInProcess = False
            createLog("Error", "general error", , ex.Message, ex.StackTrace)
        End Try
    End Sub

    'timer event every 30 mins to check if searchCompleteEvent has not fired, force stops the search to try again
    Private Sub cleanupTimer_Tick(sender As Object, e As EventArgs) Handles cleanupTimer.Tick
         If syncInProcess Then
             createLog("Debug", "stopping email search")
             If emailSearch IsNot Nothing Then
                  createLog("Warn", "email search not nothing")
                  emailSearch.Stop()
                  createLog("Warn", "after stopping email search")
             End If
         End If
    End Sub

   'handle AdvancedSearch completing and retrieve results
   Private Sub adxOutlookEvents_AdvancedSearchComplete(sender As Object, hostObj As Object) Handles adxOutlookEvents.AdvancedSearchComplete
        Dim tableTemp As Outlook.Table = Nothing

        Try
            createLog("Debug", "thread", Thread.CurrentThread.ManagedThreadId)
            If hostObj.Tag = "getEmails" Then
                Try
                    tableTemp = emailSearch.GetTable
                    MyTable = NewRefObj(tableTemp)
                    SyncEmailsBackgroundWorker.RunWorkerAsync(MyTable) 'process returned emails
                Catch ex As Exception
                   
                Finally
                    emailSearch = Nothing
                    tableTemp = Nothing
                End Try
            End If
        Catch ex As Exception
            createLog("Error", "general error", , ex.Message, ex.StackTrace)
        End Try
    End Sub

   'stop advance search and clean variable ready to try again
    Private Sub adxOutlookEvents_AdvancedSearchStopped(sender As Object, hostObj As Object) Handles adxOutlookEvents.AdvancedSearchStopped
        Try
            If hostObj.Tag = "getEmails" Then
                Try
                    createLog("Debug", "resetting variables")
                    syncInProcess = False
                    emailSearch = Nothing
                Catch ex As Exception
                    
                End Try
            End If
        Catch ex As Exception
            createLog("Error", "general error", , ex.Message, ex.StackTrace)
        End Try
    End Sub
Posted 08 Jul, 2016 09:39:23 Top
Andrei Smolin


Add-in Express team


Posts: 18829
Joined: 2006-05-11
Hello Tom,

We've never met such a situation. In what message store type you are running the search? What Outlook build is sinstalledon that machine? I would also suggest that you create a simple add-in intercepting this event and test it.

Below is the code I tested on my machine. The add-in shows a Ribbon Edit Box and a Ribbon Button. You enter the search term in the edit box and click the button. When the search is complete, the add-in shows a message box.

string advancedSearchTag = "My test advanced search in Outlook";

private void RunAdvancedSearch(Outlook._Application OutlookApp, string wordInSubject) {
    string scope = "Inbox";
    string filter = "urn:schemas:mailheader:subject LIKE '%" + wordInSubject + "%'";
    Outlook.Search advancedSearch = null;
    Outlook.MAPIFolder folderInbox = null;
    Outlook.MAPIFolder folderSentMail = null;
    Outlook.NameSpace ns = null;
    try {
        ns = OutlookApp.GetNamespace("MAPI");
        folderInbox = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
        folderSentMail = ns.GetDefaultFolder(
                                        Outlook.OlDefaultFolders.olFolderSentMail);
        scope = "'" + folderInbox.FolderPath +
                                        "','" + folderSentMail.FolderPath + "'";
        advancedSearch = OutlookApp.AdvancedSearch(
                                        scope, filter, true, advancedSearchTag);
    } catch (Exception ex) {
        MessageBox.Show(ex.Message, "An eexception is thrown");
    } finally {
        if (advancedSearch != null) Marshal.ReleaseComObject(advancedSearch);
        if (folderSentMail != null) Marshal.ReleaseComObject(folderSentMail);
        if (folderInbox != null) Marshal.ReleaseComObject(folderInbox);
        if (ns != null) Marshal.ReleaseComObject(ns);
    }
}

private void adxOutlookAppEvents1_AdvancedSearchComplete(object sender, object hostObj) {
    MessageBox.Show("adxOutlookAppEvents1_AdvancedSearchComplete");
}

private void adxRibbonButton1_OnClick(object sender, IRibbonControl control, bool pressed) {
    string searchString = this.adxRibbonEditBox1.Text;
    if (!string.IsNullOrWhiteSpace(searchString)) {
        RunAdvancedSearch(this.OutlookApp, searchString);
    } else {
        MessageBox.Show("Incorrect search string");
    }
}



Andrei Smolin
Add-in Express Team Leader
Posted 11 Jul, 2016 08:34:40 Top
tadams


Guest


Hi Andrei,

We've been able to do some more testing on this and it seems to be linked with Outlook being in non-cached mode. When changing the user to be in cached mode in Outlook the AdvancedSearch_complete event fires and all is fine, if we return back to non-cached mode then the event doesn't fire any more (at least it doesn't fire within 30 mins which is our cut-off time for resetting and trying again).

What is strange is that for a newly created test user in non-cached mode the event does fire okay, however this user only has c.50 emails in their inbox.

This makes me think that it is something to do with the exchange server when running in non-cached mode, either it is being very slow or something is preventing it from searching on certain users. We tested the Outlook search bar (in a non-cached instance) and that was able to return results in Outlook, so search is working to some degree at least.

Do you know of any settings at the exchange level we should check that might be influencing why the advancedSearch is not working?

Thanks,
Tom
Posted 20 Jul, 2016 09:40:26 Top
Andrei Smolin


Add-in Express team


Posts: 18829
Joined: 2006-05-11
Tom,

I suppose this may be caused by something broken in the .OST file; see https://support.office.com/en-us/article/Introduction-to-Outlook-Data-Files-pst-and-ost-6d4197ec-1304-4b81-a17d-66d4eef30b78#__toc290027730. At https://support.office.com/en-us/article/Repair-Outlook-Data-Files-pst-and-ost-25663bc3-11ec-4412-86c4-60458afc5253, they write:


If you encounter problems with an offline Outlook Data File (.ost), the file can be deleted and recreated by downloading a copy of your items again.



Andrei Smolin
Add-in Express Team Leader
Posted 20 Jul, 2016 09:52:56 Top
tadams


Guest


Hi Andrei,

Okay we will look into that, however as part of our testing we switched a user who had problem to cached mode and completely recreated the profile, which then solved the problem.

We then turned cached mode back off again and it stopped working once more, which is why we know it is related to non-cached.

Would switching between the different modes like this and refreshing the profile mean that the .OST file should be okay, or is the .OST file stored at exchange level and therefore may still be corrupt and the cause of the problem?

Thanks,
Tom
Posted 20 Jul, 2016 10:02:28 Top
Andrei Smolin


Add-in Express team


Posts: 18829
Joined: 2006-05-11
The .OST file is stored on the client computer; it isn't stored on the Exchange level.

Are there other stores in that profile? If there are a store of the type other than .PST I would suggest checking if turning that store off helps to solve the issue.


Andrei Smolin
Add-in Express Team Leader
Posted 20 Jul, 2016 10:14:35 Top
tadams


Guest


Hi Andrei,

Is the .OST file relevant to this problem then if it is stored on the client computer, since those links you mentioned said that the .OST is used if Outlook is operating in cached-mode. This AdvanceSearch_complete issue is only occurring when the client is not in cached-mode and therefore the search is being performed back at the exchange server and not using the .OST?

Would multiple stores in the profile still be a potential cause when in non-cached mode?

Thanks,
Tom
Posted 20 Jul, 2016 10:37:39 Top
tadams


Guest


Hi Andrei,

We are still having issues trying to get the AdvancedSearch working on this client who is using non-cached Outlook mode. Is there an alternative asynchronous method for searching all their mail folders for emails and iterating through these to extract information we need to sync? We need to account for users that might have 100,000s of emails and so typically split up the background syncing process into weekly chunks.

I've looked at maybe using restrict function, but this is quite an expensive operation to be performing on server (when in non-cached mode), especially if lots of users are doing this at once.

Thanks,
Tom
Posted 25 Jul, 2016 09:26:56 Top
Andrei Smolin


Add-in Express team


Posts: 18829
Joined: 2006-05-11
Hello Tom,

You could try to describe your situation and ask your question on the Outlook for Developers forum at https://social.msdn.microsoft.com/Forums/en-US/home?forum=outlookdev. Also you can contact Microsoft Support.

I recommend using Restrict. With non-cached mode you have no chance to perform something on the client.


Andrei Smolin
Add-in Express Team Leader
Posted 25 Jul, 2016 10:13:21 Top