Create Search Folder that filters by category RRS feed

  • Question

  • Hello, i'm working on an Outlook add-in that allows to automatically create Search folders based on some criteria, one of which is a Category. I have a server side component which creates the search folders by using the EWS managed api, however i found that while i can create the folder correctly, the criteria is not correct and can't see any items when opening the search folder , neither can i customize the folder as the Criteria button is disabled for that folder.

    Researching more into this i found that there doesn't seem to be a way to search by a multivalued property like Category, see sample below

    var searchFolder = new SearchFolder(service);

    var searchCriteria = new SearchFilter.ContainsSubstring(ItemSchema.Categories, “Customers”);


    searchFolder.SearchParameters.Traversal = SearchFolderTraversal.Shallow;

    searchFolder.SearchParameters.SearchFilter = searchCriteria;

    searchFolder.DisplayName = “Customers”;



    This doesn't work as SearchFilter.ContainsSubstring(ItemSchema.Categories, “Customers”) doesn't create a valid filter for the Search Folder in the Categories case, it does work for simple properties like Subject/Body/etc

    I've been looking on how to do this from within the add-in but so far i have found only samples that query for simple or singlevalued properties like Subject, from or body, which is not what i need.

    Is there any api or feature i can use to create an advanced filter that allows to query a multivalued property like Categories?

    I also need to save the search folder so that it is available under Search Folders, so basically i'm not looking for how to perform a query that returns results at runtime on my code, but rather to create the correct filter for a search folder that will be persisted for the user in Outlook.

    Someone suggested to use MAPI but i can't find any good current information on how i would create or modify a search folder using this approach.

    I would like to create the search folder from the server side, but if that's not possible then i can create it from within the add-in

    Thanks for reading

    Friday, February 3, 2017 9:30 PM

All replies

  • Don't know about EWS, but on the MAPI level multivalued string properties are treated as single valued string properties for the searches.

    A few things you can do on the client side

    1. Use Application.AdvancedSearch to create a search folder. You can then call Search.Save to save your search as a search folder.

    2. Redemption and its RDOSearchFolder object - search folders are usually invisible to an end user and are created outside of the IPM folder tree. You can however create a search folder anywhere.

    3. Redemption and its RDOSearch object that represents Outlook search folders under the "Search Folder" node in the Outlook folder tree view. You get full access to the searches (read existing searches, modify, create, delete).

    Dmitry Streblechenko (MVP)
    Redemption - what the Outlook
    Object Model should have been
    Version 5.5 is now available!

    Friday, February 3, 2017 9:52 PM
  • Hello,

    I'd suggest asking EWS specific questions on the Exchange Server Development forum instead.

    > I also need to save the search folder so that it is available under Search Folders

    As for the Outlook object model, you may find the AdvancedSearch method of the Application class helpful. The key benefits of using the AdvancedSearch method in Outlook are:

     - The search is performed in another thread. You don’t need to run another thread manually since the AdvancedSearch method runs it automatically in the background.
     - Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, i.e. beyond the scope of a certain folder. The Restrict and Find/FindNext methods can be applied to a particular Items collection (see the Items property of the Folder class in Outlook).
     - Full support for DASL queries (custom properties can be used for searching too). You can read more about this in the Filtering article in MSDN. To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the IsInstantSearchEnabled property of the Store class).
    - You can stop the search process at any moment using the Stop method of the Search class.

    The AdvancedSearch method and related features in the Outlook object model do not create a Search Folder that will appear in the Outlook user interface. However, you can use the Save method of the Search object that is returned to create a Search Folder that will appear in the Search Folders list in the Outlook user interface.

    Read more about that in the Advanced search in Outlook programmatically: C#, VB.NET article.

    The Categories property returns a String representing the categories assigned to the Outlook item. So, you just need to create a filter which allows to find a substring, see Filtering Items Using a String Comparison for more information.

    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Friday, February 3, 2017 10:51 PM
  • Thanks for the replies, i have moved to create the search folder on the add-in, however i'm still seeing problems when searching for categories, here's a sample of my current code that works

    string scope = "'Inbox', 'Sent Items'";
    string filter = "urn:schemas:mailheader:subject LIKE '%Diagnostics%'";
    var avFolder = Application.AdvancedSearch(scope, filter, true, "CustomersQueryTag");

    This works well and i can see items on the search folder that contain Diagnostics on the subject.

    After this i tried changing the filter to search by category, based on what i read about filtering using string and keyword comparisons 

    string scope = "'Inbox', 'Sent Items'";
    var filter = "\"urn:schemas-microsoft-com:office:office#Keywords\" = 'Customers'";
    var avFolder = Application.AdvancedSearch(scope, filter, true, "CustomersQueryTag");

    Which i got from the microsoft docs here Filtering Items Using a Comparison with a Keywords Property

    You can also use a DASL query for equivalence matching in a multi-valued string property. Consider an example where items have one or more of the following four categories:

    Book My Book Book Review Bookish

    The DASL equivalence query:

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

    returns any item that has Book as a category, including those categorized with multiple categories, where Book is one of the categories. The query does not return items that do not have Book as a category.

    And this doesn't work, i get an empty folder, i also tried with 'Category' but this doesn't seem to have any effect on the query

    var filter = "\"urn:schemas-microsoft-com:office:office#Category\" = 'Customers'";

    I found this property here Category Field but everywhere else i looked samples seem to indicate that to search for a category i have to use '#Keywords'

    At this point i can't find any more documentation on how to filter by category, it seems weird that the Microsoft documentation specifies one way to do it that it doesn't really work.

    Does anyone knows what filter could be useful to search by category? it seems to be the only problem i have at this time, because filtering by any other property like subject/body works perfectly.

    As before, if i create the search folder from Outlook and set the category to be "Customers" then i can see the messages. It only fails when creating the search folder from within the add-in so my guess is the search filter is incorrect, however i don't get any errors and i have found it very hard to find samples that do it different than what I've tried so far.


    • Edited by kaifan Friday, February 10, 2017 12:05 AM
    Friday, February 10, 2017 12:03 AM
  • Hi,

    The DASL query for categories using '#Keywords' works fine for me.

    Sub CategoryRestriction()
        Dim oFolder As Outlook.Folder
        Dim oItems As Outlook.Items
        Dim Filter As String
        Filter = "@SQL=" & Chr(34) & _
            "urn:schemas-microsoft-com:office:office#Keywords" & _
            Chr(34) & "='Blue Category'"
        Set oFolder = Application.ActiveExplorer.CurrentFolder
        Set oItems = oFolder.Items.Restrict(Filter)
        Debug.Print oItems.Count
    End Sub

    I suggest you filter manually to check if there is mailitem flagged with some categories.



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Thursday, February 16, 2017 7:13 AM