none
Using c# AdvancedSearch method for contacts RRS feed

  • Question

  • I am developing a windows form application and need to programmically replicate through c# an advanced search method so that it would be easier for my users as typically they might want to search for the following:

    Categories  contains ROLX
    E-mail  contains @
    ZIP/Postal Code word starts with 105
    User Field 2 contains B-H or User Field 2 contains B-O

    While I see the AdvancedSearch method is discussed in http://msdn.microsoft.com/en-us/library/office/ff866933(v=office.14).aspx as the part I'm stuck on is how do I appropriately built my command so it replicates the above search properly.  The scope will always assume current folder location which will be my custom contact folder, no subfolders nor tags at least.  I need the filter to be flexible enough like the example I provided.  In my search for an example I could not seem to find one I could understand and attempts to try only gave me syntax errors.  Please help, thanks.

    Saturday, November 10, 2012 9:53 PM

Answers

  • This is getting intriguing, When I set my scope to a personal folder called "Rolodex

         iscope = "Rolodex";

    I don't get an error but I don't see where my search results are unless there's something extra I need to do.  When I search on a public folder


                iscope = @"\Public Folders - JFalberg@accucomci.com\All Public Folders\Accucom Contacts";

    is when I do get the error message
    • Edited by jfalberg Tuesday, November 13, 2012 2:53 PM pressed submit too soon.
    • Marked as answer by Quist ZhangModerator Tuesday, November 20, 2012 11:12 AM
    Tuesday, November 13, 2012 2:52 PM
  • If you want the search results to be displayed in Outlook, than you must use Explorer.Search but you won't get direct access to the results in code.

    Otherwise, for AdvancedSearch try a Public Folder search without searching subfolders - that may not be supported.  There's also an example similar to your situation here:

    OutlookCode.com :: Advanced Search:
    http://www.outlookcode.com/threads.aspx?forumid=2&messageid=23100


    Eric Legault MVP (Outlook)
    About me...
    Try Outlook Appins - affordable and easy to use!

    Tuesday, November 13, 2012 3:44 PM
    Moderator

All replies

  • Did you follow the guidelines for building DASL criteria on this page?

    Filtering Items:
    http://msdn.microsoft.com/en-us/library/office/ff863965(v=office.14).aspx

    It would help if you show your code.


    Eric Legault MVP (Outlook)
    About me...
    Try Outlook Appins - affordable and easy to use!

    Monday, November 12, 2012 3:32 PM
    Moderator
  • In an attempt to display code, I am getting missing type error messages when I try to enter the following:

                Outlook.Explorer explorer = olApp.Explorers.Add(olApp.ActiveExplorer().CurrentFolder as Outlook.Folder,
                Outlook.OlFolderDisplayMode.olFolderDisplayNormal);

                filter = "[LastName] like 'Falb%'";
                olApp.AdvancedSearch(explorer.CurrentFolder, filter); 

    My next attempt will be to constrict a filter such as "[LastName] like 'Falb%' and [Email] like '%@%'" to that affect.

    Monday, November 12, 2012 4:11 PM
  • You are using a lot of chained calls there, which should be cleaned up first.  Use separate variables, like this:

    Explorers explorers = olApp.Explorers;
    Explorer activeExplorer = olApp.ActiveExplorer;
    Explorer newExplorer;
    Folder currentFolder = activeExplorer.CurrentFolder;
    
    newExplorer = explorers.Add(currentFolder, Outlook.OlFolderDisplayMode.olFolderDisplayNormal);
    etc.

    Eric Legault MVP (Outlook)
    About me...
    Try Outlook Appins - affordable and easy to use!

    Monday, November 12, 2012 4:21 PM
    Moderator
  • at top I have

    using Outlook = Microsoft.Office.Interop.Outlook;

    By adding the following:

                Outlook.Explorers explorers = olApp.Explorers;
                Outlook.Explorer activeExplorer = olApp.ActiveExplorer;
                Outlook.Explorer newExplorer;
                Outlook.Folder currentFolder = activeExplorer.CurrentFolder;

    I'm not sure why I'm getting the red curly lines indicating for the activeExplorer line as

    Error 2 Cannot implicitly convert type 'Microsoft.Office.Interop.Outlook.MAPIFolder' to 'Microsoft.Office.Interop.Outlook.Folder'. An explicit conversion exists (are you missing a cast?) D:\devel\dmlawyers\TM2Outlook\DMAdvancedSearch1\DMAdvancedSearch1\Form1.cs 95 44 DMAdvancedSearch1

    I also had the following defined as well:

                Outlook.Application olApp = new Outlook.Application();
                Outlook.NameSpace olNS = olApp.GetNamespace("MAPI");
                Outlook.MAPIFolder rootPublicfolder = olNS.GetDefaultFolder(Outlook.OlDefaultFolders.olPublicFoldersAllPublicFolders);
                           

    Monday, November 12, 2012 4:30 PM
  • It looks like you have a reference set to the Outlook 2003 Object Model/PIA: MAPIFolder was renamed to Folder starting with Outlook 2007.  Just switch to using MAPIFolder throughout.

    Eric Legault MVP (Outlook)
    About me...
    Try Outlook Appins - affordable and easy to use!

    Monday, November 12, 2012 4:51 PM
    Moderator
  • So far this seems to clear some errors until I get to my AdvancedSearch line:

                Outlook.Explorers explorers = olApp.Explorers;
                Outlook.Explorer activeExplorer = olApp.ActiveExplorer();
                Outlook.Explorer newExplorer;
                Outlook.MAPIFolder currentFolder = activeExplorer.CurrentFolder;

                newExplorer = explorers.Add(currentFolder, Outlook.OlFolderDisplayMode.olFolderDisplayNormal);

                string filter;
                filter = "[LastName] like 'Falb%'";
                olApp.AdvancedSearch(currentFolder, filter);

    Error 1 The best overloaded method match for 'Microsoft.Office.Interop.Outlook._Application.AdvancedSearch(string, object, object, object)' has some invalid arguments D:\devel\dmlawyers\TM2Outlook\DMAdvancedSearch1\DMAdvancedSearch1\Form1.cs 101 13 DMAdvancedSearch1

    Error 2 Argument 1: cannot convert from 'Microsoft.Office.Interop.Outlook.MAPIFolder' to 'string' D:\devel\dmlawyers\TM2Outlook\DMAdvancedSearch1\DMAdvancedSearch1\Form1.cs 101 34 DMAdvancedSearch1

    Monday, November 12, 2012 5:17 PM
  • You are using the wrong kind of parameters - none of them take objects.  The first Scope argument takes a folder path, not a MAPIFolder object.

    Application.AdvancedSearch Method (Outlook):
    http://msdn.microsoft.com/en-us/library/ff866933(office.14).aspx


    Eric Legault MVP (Outlook)
    About me...
    Try Outlook Appins - affordable and easy to use!

    Monday, November 12, 2012 5:26 PM
    Moderator
  • My target, being a public contact folder as
    [Public Folders - <user's email address>][All Public Folders][D&M Rolodex]
    is assuming to be the current folder. 

    I wonder if I have to string it out in a certain way somehow.

    Monday, November 12, 2012 5:37 PM
  • If you were using Outlook 2007 you could just read Folder.FolderPath.  Note also that in Outlook 2010 the paths to Public Folders are different.  From the KB: "The top-level folder for public folders was changed from "Public Folders" to "Public folders - <username>." This change was made to support having more than one public folder store that is loaded in a single MAPI profile. To update solutions so that they can work with Outlook 2010, you can retrieve the Session.CurrentUser object to build the path that has the user's name".


    Eric Legault MVP (Outlook)
    About me...
    Try Outlook Appins - affordable and easy to use!

    Monday, November 12, 2012 6:52 PM
    Moderator
  • Here's an update on my coding as I'm trying with my development environment:

                string iscope;
                iscope = @"\Public Folders - JFalberg@accucomci.com\All Public Folders\Accucom Contacts";
                string filter;
                filter = "[LastName] like 'Falb%'";
                string advancedSearchTag = "Our first advanced search in Outlook"; 
                olApp.AdvancedSearch(iscope, filter, false, advancedSearchTag);

    When I hit the last line, here's a snippet of the error I'm now getting so as close as I am getting I still feel far from accomplishing:

    System.ArgumentException was unhandled
      HResult=-2147024809
      Message=Could not complete the operation. One or more parameter values are not valid.
      Source=Microsoft Outlook
      StackTrace:
           at Microsoft.Office.Interop.Outlook.ApplicationClass.AdvancedSearch(String Scope, Object Filter, Object SearchSubFolders, Object Tag)

    Monday, November 12, 2012 6:52 PM
  • If you were using Outlook 2007 you could just read Folder.FolderPath.  Note also that in Outlook 2010 the paths to Public Folders are different.  From the KB: "The top-level folder for public folders was changed from "Public Folders" to "Public folders - <username>." This change was made to support having more than one public folder store that is loaded in a single MAPI profile. To update solutions so that they can work with Outlook 2010, you can retrieve the Session.CurrentUser object to build the path that has the user's name".


    Eric Legault MVP (Outlook)
    About me...
    Try Outlook Appins - affordable and easy to use!

    Monday, November 12, 2012 6:53 PM
    Moderator
  • I would try the same search in a Mailbox and not a Public Folder just to rule things out.  I'm surprised Public Folders are supported for AdvancedSearch, although I don't see any references that they are or are not supported. 

    Eric Legault MVP (Outlook)
    About me...
    Try Outlook Appins - affordable and easy to use!

    Monday, November 12, 2012 6:56 PM
    Moderator
  • Hmm, I tried using my mailbox with "Inbox" and "Rolodex" and still got the same message.  Good to know about the Session.CurrentUser object as I was tediously getting that.  This development is for Outlook 2010.

    Monday, November 12, 2012 7:10 PM
  • Just try using the search samples from the MSDN links I've been giving you to test first; then amend your code to use DASL props and not Jet.  You ARE reading the links I'm providing I hope...


    Eric Legault MVP (Outlook)
    About me...
    Try Outlook Appins - affordable and easy to use!

    Monday, November 12, 2012 7:17 PM
    Moderator
  • You cannot use a SQL-type operator such as "like %" with Jet syntax (filter = "[LastName] like 'Falb%'";
    ).
     
    If you want to use "like %" use the DASL property tag and syntax for [LasName], and use "@SQL=" syntax if needed.

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "jfalberg" <=?utf-8?B?amZhbGJlcmc=?=> wrote in message news:c8d47a4a-bfdc-44c6-b218-4fe2af873c86...

    Hmm, I tried using my mailbox with "Inbox" and "Rolodex" and still got the same message.  Good to know about the Session.CurrentUser object as I was tediously getting that.  This development is for Outlook 2010.


    Ken Slovak MVP - Outlook
    Monday, November 12, 2012 7:18 PM
    Moderator
  • Hopefully this was the correct link: http://msdn.microsoft.com/en-us/library/office/ff867646(v=office.14).aspx

    as I slightly changed my filter to

                filter = "[LastName] = 'Smith'";

    and still got an error message.  I see there were references to schema and was wondering if they were specific to "Subject" in the inbox section.  So to construct my filter to be as flexible as the advanced search,  ideally be like this following example?:

    criteria = "@SQL=" & Chr(34) _ & "urn:schemas-microsoft-com:office:office#Company" & Chr(34) _
    & " = 'Microsoft'"

    I feel like I'm misunderstanding something here.


    • Edited by jfalberg Monday, November 12, 2012 7:34 PM
    Monday, November 12, 2012 7:33 PM
  • You have to refer to the properties by their namespace for a filter used with AdvancedSearch, which is why [LastName] doesn't work as that's a Jet property reference format.  Also, urn:schemas-microsoft-com:office:office is specific to DocumentItem objects and not what you want.  I believe this is the property name for CompanyName in a Contact, if that's the property you want to search on:

    http://schemas.microsoft.com/mapi/proptag/0x3A16001F

    See also:

    Referencing Properties by Namespace:
    http://msdn.microsoft.com/en-us/library/office/ff868915(v=office.14).aspx


    Eric Legault MVP (Outlook)
    About me...
    Try Outlook Appins - affordable and easy to use!

    Monday, November 12, 2012 8:00 PM
    Moderator
  • I now have my coding as follows:

                string iscope;
                iscope = @"\Public Folders - JFalberg@accucomci.com\All Public Folders\Accucom Contacts";
                string filter;
                string flt;
                flt = "Falb";
                filter = "urn:schemas:contacts:sn LIKE \'%" + flt + "%\'";
                string advancedSearchTag = "Our first advanced search in Outlook"; 
                olApp.AdvancedSearch(iscope, filter, false, advancedSearchTag);

    While I don't get an error, I wonder how do I view my results.  Once I get this to work as desired I will at least want to combined conditions like I originally stated as well as the ability to export my results customly to an excel file through excel automation.


    • Edited by jfalberg Monday, November 12, 2012 9:03 PM
    Monday, November 12, 2012 9:03 PM
  • spoke too soon, still got errors:

    System.ArgumentException was unhandled
      HResult=-2147024809
      Message=Could not complete the operation. One or more parameter values are not valid.
      Source=Microsoft Outlook
      StackTrace:
           at Microsoft.Office.Interop.Outlook.ApplicationClass.AdvancedSearch(String Scope, Object Filter, Object SearchSubFolders, Object Tag)
           at DMAdvancedSearch1.Form1.button1_Click(Object sender, EventArgs e) in

    Monday, November 12, 2012 9:11 PM
  • This is getting intriguing, When I set my scope to a personal folder called "Rolodex

         iscope = "Rolodex";

    I don't get an error but I don't see where my search results are unless there's something extra I need to do.  When I search on a public folder


                iscope = @"\Public Folders - JFalberg@accucomci.com\All Public Folders\Accucom Contacts";

    is when I do get the error message
    • Edited by jfalberg Tuesday, November 13, 2012 2:53 PM pressed submit too soon.
    • Marked as answer by Quist ZhangModerator Tuesday, November 20, 2012 11:12 AM
    Tuesday, November 13, 2012 2:52 PM
  • If you want the search results to be displayed in Outlook, than you must use Explorer.Search but you won't get direct access to the results in code.

    Otherwise, for AdvancedSearch try a Public Folder search without searching subfolders - that may not be supported.  There's also an example similar to your situation here:

    OutlookCode.com :: Advanced Search:
    http://www.outlookcode.com/threads.aspx?forumid=2&messageid=23100


    Eric Legault MVP (Outlook)
    About me...
    Try Outlook Appins - affordable and easy to use!

    Tuesday, November 13, 2012 3:44 PM
    Moderator
  • See if this VBA sample for public folders helps: http://www.outlookcode.com/codedetail.aspx?id=1253

    --
    Ken Slovak
    [MVP-Outlook]
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
    "jfalberg" <=?utf-8?B?amZhbGJlcmc=?=> wrote in message news:7e739ef3-b679-45a8-9ec1-5e9094c9b4be...

    This is getting intriguing, When I set my scope to a personal folder called "Rolodex

         iscope = "Rolodex";

    I don't get an error but I don't see where my search results are unless there's something extra I need to do.  When I search on a public folder


                iscope = @"\Public Folders - JFalberg@accucomci.com\All Public Folders\Accucom Contacts";

    is when I do get the error message

    Ken Slovak MVP - Outlook
    Tuesday, November 13, 2012 3:46 PM
    Moderator