What is the Kentico 12 analog for "SearchContext.CurrentSearchResults.Count"?

Targutai Yesugei asked on November 21, 2019 11:29

In Kentico 11 I used SearchContext.CurrentSearchResults.Count to count search result.

It was placed to OnOnSearchCompleted event handler. Now while upgrade it says, that Class CMS.Search.SearchContext was removed.

How can I replace it in Kentico 12?

Recent Answers


Roman Hutnyk answered on November 21, 2019 13:48

According to documentation you need to use members of corresponding CMS.Search.SearchResult or CMS.Search.SearchResultItem instead.

You can see all API changes @ https://devnet.kentico.com/documentation/api-changes/kentico-12?page=1

Also you might check Kentico API classes @ http://devnet.kentico.com/docs/12_0/api/html/N_CMS_Search.htm

Hope that helps

0 votesVote for this answer Mark as a Correct answer

Targutai Yesugei answered on November 21, 2019 14:16

Hello, Roman. Thank you.

I saw this suggest in Kentico upgrade tool, but it says nothing. Do I need to change the code paradigm. So, if in Kentico 11 I used a static class in event, in Kentico 12 I should do something completely different? As I see one-to-one change isn't enough and I can't understand what should I do.

0 votesVote for this answer Mark as a Correct answer

Michal Samuhel answered on November 21, 2019 14:57

Its hard to say without full context, but OnSearchCompleted is raised by search results control:

srchResults.OnSearchCompleted += srchResults_OnSearchCompleted;

So how about utilizing event and control data source to count number of items:

int count = srchResults.DataSource.Tables[0].Rows.Count;

0 votesVote for this answer Mark as a Correct answer

Targutai Yesugei answered on November 22, 2019 06:12

Hello, Michal, thank you.

Sorry, here is my code:

        <project:SearchResults ID="SearchResultList" OnOnSearchCompleted="SearchResultList_OnOnSearchCompleted" IgnoreTransformations="True" NoResultsText="" runat="server">
      <ItemTemplate>
        <%# SetRowContext(Container) %>
        <div>
            <%# HighlightFoundText(SearchResultRow.SearchResultItem, SearchResultRow.Teaser) %>             
        </div>
      </ItemTemplate>
    </project:SearchResults>

Here is SetRowContext method

protected string SetRowContext(Control container)
    {
        var searchResultItem = (SearchResultItem)(container as RepeaterItem)?.DataItem;

        if (searchResultItem == null) return string.Empty;

        SearchResultRow = new SmartSearchResultRow(searchResultItem, SmartSearchContext, DocumentProvider);

        return string.Empty;
    }

And there is OnOnSearchCompleted event handler (from Kentico 11)

    protected void SearchResultList_OnOnSearchCompleted(bool visible)
    {
        var numberOfResults = SearchContext.CurrentParameters?.NumberOfResults ?? 0;
        ResultsFound.Text = numberOfResults.ToString();            
    }

So, when I added object sender parameter to OnOnSearchCompleted event handler, in ascx file, in row <project:SearchResults ID="SearchResultList" OnOnSearchCompleted="SearchResultList_OnOnSearchCompleted" IgnoreTransformations="True" NoResultsText="" runat="server"> it says that Parameters do not match to the method signature

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on November 22, 2019 08:54

The direct replacement for CMS.Search.SearchContext.CurrentSearchResults.Count is CMS.Search.SearchResult.Items.Count.
It can be accessed also through the SearchResultItem: CMS.Search.SearchResultItem.Result.Items.Count

An example of ascx transformation that would return the number of results on current page:

<%# ((CMS.Search.SearchResultItem)DataItem).Result.Items.Count %>

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.