SearchSuggestionCollection SearchSuggestionCollection SearchSuggestionCollection SearchSuggestionCollection SearchSuggestionCollection Class

Definition

Represents a collection of search suggestions to be displayed in the search pane in response to a suggestionsrequested event.

public : sealed class SearchSuggestionCollection : ISearchSuggestionCollection
struct winrt::Windows::ApplicationModel::Search::SearchSuggestionCollection : ISearchSuggestionCollection
public sealed class SearchSuggestionCollection : ISearchSuggestionCollection
Public NotInheritable Class SearchSuggestionCollection Implements ISearchSuggestionCollection
// This class does not provide a public constructor.
Attributes
Windows 10 requirements
Device family
Windows 10 (introduced v10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced v1)

Examples

The Search contract sample demonstrates how to respond to a suggestionsrequested event, including how to use the searchSuggestionCollection.

private void OnSearchPaneSuggestionsRequested(SearchPane sender, SearchPaneSuggestionsRequestedEventArgs e)
{
    var queryText = e.QueryText;
    if (string.IsNullOrEmpty(queryText))
    {
        MainPage.Current.NotifyUser("Use the search pane to submit a query", NotifyType.StatusMessage);
    }
    else
    {
        var request = e.Request;
        var linguisticDetails = e.LinguisticDetails;
        foreach (string alternative in linguisticDetails.QueryTextAlternatives)
        {
            foreach (string suggestion in suggestionList)
            {
                if (suggestion.StartsWith(alternative, StringComparison.CurrentCultureIgnoreCase))
                {
                    // Add suggestion to Search Pane
                    request.SearchSuggestionCollection.AppendQuerySuggestion(suggestion);

                    // Break since the Search Pane can show at most 5 suggestions
                    if (request.SearchSuggestionCollection.Size >= MainPage.SearchPaneMaxSuggestions)
                    {
                        break;
                    }
                }
            }

            // Break since the Search Pane can show at most 5 suggestions
            if (request.SearchSuggestionCollection.Size >= MainPage.SearchPaneMaxSuggestions)
            {
                break;
            }
        }

        if (request.SearchSuggestionCollection.Size > 0)
        {
            MainPage.Current.NotifyUser("Suggestions provided for query: " + queryText, NotifyType.StatusMessage);
        }
        else
        {
            MainPage.Current.NotifyUser("No suggestions provided for query: " + queryText, NotifyType.StatusMessage);
        }
    }
}
// Register the onsuggestionsrequested event in your apps global scope, for example default.js, so that it is registered as soon as your app is launched.
Windows.ApplicationModel.Search.SearchPane.getForCurrentView().onsuggestionsrequested = function (eventObject) {
    var queryText = eventObject.queryText, suggestionRequest = eventObject.request, linguisticDetails = eventObject.linguisticDetails;

    var queryAlternatives = linguisticDetails.queryTextAlternatives;
    var maxNumberOfSuggestions = 5;
    for (var i = 0, len = queryAlternatives.length, done = false; i < len && !done; i++) {
        // toLowerCase not necessary for East Asian languages. Preserves compatibility when non East Asian suggestions are mixed in with East Asian suggestions.
        var alternative = queryAlternatives[i].toLowerCase();
        for (var j = 0, suggestionLength = suggestionList.length; j < suggestionLength; j++) {
            if (suggestionList[j].substr(0, alternative.length).toLowerCase() === alternative) {
                suggestionRequest.searchSuggestionCollection.appendQuerySuggestion(suggestionList[j]);
                if (suggestionRequest.searchSuggestionCollection.size === maxNumberOfSuggestions) {
                    done = true;
                    break;
                }
            }
        }
    }

    // Construct list of alternatives so we can output them.
    var alternatives = "";
    for (var k = 0, alternativeCount = queryAlternatives.length; k < alternativeCount; k++) {
        alternatives += queryAlternatives[k];
    }
    if (suggestionRequest.searchSuggestionCollection.size > 0) {
        WinJS.log && WinJS.log("Suggestions provided for query: " + queryText + "\nUsing alternatives: " + alternatives, "sample", "status");
    } else {
        WinJS.log && WinJS.log("No suggestions provided for query: " + queryText + "\nUsing alternatives: " + alternatives, "sample", "status");
    }
};

Remarks

The search pane can display 5 suggestions, at most. Additionally, each separator you use takes the place of a suggestion and lowers the number of suggestions that you can display. Learn more about displaying suggestions in Guidelines and checklist for search.

An app retrieves this object through the searchPaneSuggestionsRequest.searchSuggestionCollection property while handling a suggestionsrequested event.

Properties

Size Size Size Size Size

The number of suggestions in the collection.

Methods

AppendQuerySuggestion(String) AppendQuerySuggestion(String) AppendQuerySuggestion(String) AppendQuerySuggestion(String) AppendQuerySuggestion(String)

Appends a query suggestion to the list of search suggestions for the search pane.

AppendQuerySuggestions(IIterable<String>) AppendQuerySuggestions(IIterable<String>) AppendQuerySuggestions(IIterable<String>) AppendQuerySuggestions(IIterable<String>) AppendQuerySuggestions(IIterable<String>)

Appends a list of query suggestions to the list of search suggestions for the search pane.

AppendResultSuggestion(String, String, String, IRandomAccessStreamReference, String) AppendResultSuggestion(String, String, String, IRandomAccessStreamReference, String) AppendResultSuggestion(String, String, String, IRandomAccessStreamReference, String) AppendResultSuggestion(String, String, String, IRandomAccessStreamReference, String) AppendResultSuggestion(String, String, String, IRandomAccessStreamReference, String)

Appends a suggested search result to the list of suggestions to display in the search pane.

AppendSearchSeparator(String) AppendSearchSeparator(String) AppendSearchSeparator(String) AppendSearchSeparator(String) AppendSearchSeparator(String)

Appends a text label that is used to separate groups of suggestions in the search pane.

See Also