Enable empty SP search textbox does not update results

Hi Guys,
This post is about enabling empty search queries. By default, when user press 'Enter' on an empty Search Box, nothing happens.  A very simple question might be asked that why would anyone search for empty string any way?  True, no one actually needs it desperately but while going in flow this case could cause user bed experiences because users are likely to start with the refiners.

Problem:
Consider this flow:
  1. Navigate to SharePoint Search page
  2. Search with any keyword like "Test"
  3. If search results are not relevant, I just removed Test keyword from the textbox and hit search button.
  4. Nothing actually happens here. It suppose to clear all existing results.
Without the ability to search for the empty string (or a "clear" button), the only way to do same would be to reload the page.

Solution: 
The Search Box webpart supports empty string search. Yes, its true, it's just disabled by default.  This feature is controlled by a property, AllowEmptySearch, which can not be managed from the UI. This fix is rather easy. This is a property that can be set on the search box, by default set to false. Follow the given instructions to update this property:
  1. Edit the page where search box is placed.
  2. Export your Search Box webart.
  3. Edit the .webpart file using your favorite text editor
  4. Search for the property AllowEmptySearch and update its value to True.
  5. Now import this web part to gallery and add it to your page in place of default search box web part.
This functionality is necessary when you have pages on which you configured the search web parts to use predefined queries in order to show results when the page gets loaded.
When you update this property to true, which can be done by exporting the search box web part, and changing the following line in the XML:
<!-- old -->
<property name="AllowEmptySearch" type="bool">False</property>
 
<!-- new -->
<property name="AllowEmptySearch" type="bool">True</property>
 
Note: you can also update the web part with PowerShell.
Another way to approach it is via setting this property from within your display template. To update the property within the search box display template, you have to add the following lines of code:
// Allow empty keyword searches
if (!ctx.ClientControl.get_allowEmptySearch()) {
    ctx.ClientControl.set_allowEmptySearch(true);
}
 
Hope this small trick helps you solve this issue.

Happy SharePointing !

Ref: Enable empty SP search textbox does not update results

Comments

Popular posts from this blog

Hide Ribbon on SharePoint 2013 using CSS

Get Comment Count in SharePoint

Configure external site as content sources in sharepoint search