0
votes

I'm looking to find a way to hide the whole Content Search webpart (including the webpart title and border area) if there are no results coming back from the query.

What I tried

  1. There is this property: "ShouldHideControlWhenEmpty" but it doesn't do anything when there are no results. The webpart is still visible. Webpart has a the Title and the Border around it so when there are no results the title and the border show up with no results. Like I said I want to hide the whole thing in that case.

  2. Wrote a jQuery script that searches for the text "There are no results" message (which comes from the webpart when there are no results) and hides the webpart. However, there is a short flicker once the page loads because the webpart first gets rendered for a split second and then disappears by virtue of my jQuery script - which is not great UI experience.

  3. So I thought I could extend the OOTB ContentBySearchWebPart and get a hold of some kind property that would get me the result count. If the results count is 0 simply I would hide the webpart server side. I looked at the documentation online - nothing I could find that I could leverage. Help? Thanks!

1

1 Answers

0
votes

The option 2 using jquery would be fine, but just flip the approach you are doing now. Instead of hiding the webpart if its empty , make the webpart hidden by default, and visible only when content is available. Here is the approach.

  1. Add a Content Editor / Script Editor webpart with CSS code that hides(remember just mark display none / visible false, dont remove) the Content Search Webpart by default. Place the styling code above the Content search webpart to ensure when the page loads the Content search Webpart is not visible.
  2. During pageload check if the Content search webpart contains result , if the webpart contains result , then make the webpart visible. Here you will search for the text "There are no results" message, if its present you do nothing , else you make the webpart visible as it means there are search results available.

This way you will eliminate the Flicking problem with the current approach.