Adrian Bannister
Adrian Bannister

Reputation: 523

Hyperlink to page with smart search filter prepopulated

I have a smart search page that shows all the products on the page with some smart search filters that narrows down the products on some criterias (Let's say for example Filter1 has Option1, Option2 and Option3).

What I am trying to accomplish is to have a link on a seperate page that links to the product page, but when the user clicks on that link some of the search filters gets set (For example Filter1 would have Option2 selected).

Upvotes: 0

Views: 156

Answers (2)

Peter Mogilnitski
Peter Mogilnitski

Reputation: 998

For Smart Search Filters: if turn off auto-post back option -then web part control ID should become a query string parameter that you can use.enter image description here

This above will form something like: /Smart-search-filter.aspx?searchtext=abc&searchmode=anyword&wf=2;&ws=0;&wa=0

P.S. I suggest you to take a look at the corporate site example: look the smart search filter web part: /Examples/Web-parts/Full-text-search/Smart-search/Smart-search-filter. It is working example you can use it as starting point.

Upvotes: 1

Dragoljub Ilic
Dragoljub Ilic

Reputation: 247

I'm not sure if that is possible with out of the box solution, but with simple tweaks inside SearchFilter.ascx.cs, you can make a workaround. File is placed under CMSWebParts/SmartSearch/SearchFilter.ascx.cs. You should change method 'GetSelectedItems' to take a look into query string for filter value (see snippet bellow):

/// <summary>
/// Gets selected items.
/// </summary>
/// <param name="control">Control</param>  
/// <param name="ids">Id's of selected values separated by semicolon</param>
private string GetSelectedItems(ListControl control, out string ids)
{
    ids = "";
    string selected = "";

    //CUSTOM: retrive value for query string
    var customFilter = QueryHelper.GetString("customFilter", "");

    // loop through all items
    for (int i = 0; i != control.Items.Count; i++)
    {
        //CUSTOM: ----START-----
        if (!RequestHelper.IsPostBack())
        {
            if (!string.IsNullOrEmpty(customFilter))
            {
                if (control.Items[i].Text.Equals(customFilter, StringComparison.InvariantCultureIgnoreCase))
                {
                    control.Items[i].Selected = true;
                }
            }
        }
        //CUSTOM: ----END-----

        if (control.Items[i].Selected)
        {
            selected = SearchSyntaxHelper.AddSearchCondition(selected, control.Items[i].Value);
            ids += ValidationHelper.GetString(i, "") + ";";
        }
    }

    if (String.IsNullOrEmpty(selected) && (control.SelectedItem != null))
    {
        selected = control.SelectedItem.Value;
        ids = control.SelectedIndex.ToString();
    }

    return selected;
}

And your hyperlink will look like this: /Search-result?searchtext=test&searchmode=anyword&customfilter=coffee

With this modifications, you can send only one value in filter, but if you need more then one value, you can send them and customize it however suits you best. Also, you can send filter name (in case that you have multiple filters) and then add check in method above.

I will recommend you not to modify kentico files. Instead of that, clone default filter web part and make modifications there, because withing next upgrade of project, you will lose your changes. I checked this in Kentico 11.

Upvotes: 1

Related Questions