Outlook IsInstantSearchEnabled Property Value Changes

196 views Asked by At

I am trying to speed up my Advanced Search by using ci_phrasematch instead of the LIKE operator. ci_phrasematch can only be used if IsInstantSearchEnabled is true. If Outlook is open and running the first time my code executes IsInstantSearchEnabled = false. If I run my code again without closing Outlook the second time IsInstantSearchEnabled = true, but then my Advanced Search blows up with "System.Runtime.InteropServices.COMException: 'Cannot use content indexer keywords in the restriction. Instant search is not enabled on this store.'"

My installed version is Outlook 365 using KOFF (Offline Kerio Outlook Connector) to sync with the server.

This is my first time working with Microsoft.Office.Interop.Outlook. Am I missing something here?

`

using Outlook = Microsoft.Office.Interop.Outlook;

Type officeType = Type.GetTypeFromProgID("Outlook.Application");

                if (officeType != null) //outlook is installed
                {
                    emails = new DataTable();
                    emails.Columns.Add("subject", typeof(string));
                    emails.Columns.Add("from", typeof(string));
                    emails.Columns.Add("to", typeof(string));
                    emails.Columns.Add("date", typeof(DateTime));
                    emails.Columns.Add("body", typeof(string));



                    Outlook.Application outApp = null;

                    // Check whether there is an Outlook process running.
                    if (Process.GetProcessesByName("OUTLOOK").Count() > 0)
                    {
                        outApp = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application");
                    }
                    else
                    {
                        outApp = new Outlook.Application();
                        Outlook.NameSpace nameSpace = outApp.GetNamespace("MAPI");
                        nameSpace.Logon("", "", Missing.Value, Missing.Value);
                        nameSpace = null;
                    }


                    string scope = "'" + outApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).FolderPath + "','"
                        + outApp.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail).FolderPath + "'";

                    string email = "[email protected]";

                    string filter;

                    string contactName = GetConatactName(email);

                    if (outApp.Session.DefaultStore.IsInstantSearchEnabled)
                    {
                        if (contactName.Trim() == "")
                        {
                            filter = "(urn:schemas:httpmail:fromemail = '" + email + "' AND urn:schemas:httpmail:datereceived > '8/1/2022')"
                                + " OR (urn:schemas:httpmail:displayto ci_phrasematch '" + email + "' AND urn:schemas:httpmail:date > '8/1/2022')";
                        }
                        else
                        {
                            filter = "(urn:schemas:httpmail:fromemail = '" + email + "' AND urn:schemas:httpmail:datereceived > '8/1/2022')"
                                + " OR ((urn:schemas:httpmail:displayto ci_phrasematch '" + email + "' OR urn:schemas:httpmail:displayto ci_phrasematch '" + contactName + "')"
                                + " AND urn:schemas:httpmail:date > '8/1/2022')";
                        }
                    }
                    else
                    {
                        if (contactName.Trim() == "")
                        {
                            filter = "(urn:schemas:httpmail:fromemail = '" + email + "' AND urn:schemas:httpmail:datereceived > '8/1/2022')"
                                + " OR (urn:schemas:httpmail:displayto LIKE '%" + email + "%' AND urn:schemas:httpmail:date > '8/1/2022')";
                        }
                        else
                        {
                            filter = "(urn:schemas:httpmail:fromemail = '" + email + "' AND urn:schemas:httpmail:datereceived > '8/1/2022')"
                                + " OR ((urn:schemas:httpmail:displayto LIKE '%" + email + "%' OR urn:schemas:httpmail:displayto LIKE '%" + contactName + "%')"
                                + " AND urn:schemas:httpmail:date > '8/1/2022')";
                        }
                    }



                    outApp.AdvancedSearchComplete += OutApp_AdvancedSearchComplete;

                    outApp.AdvancedSearch(scope, filter, true, "Search");
                  }

0

There are 0 answers