Sunday, March 2, 2014

Programmatically avoid sending queries to other search web parts on the same page

Let’s assume that we have a SharePoint page where we have multiple search web parts.

In this example I have a “Search Result” web part as well as a “Search Box” web part those are added to the page programmatically. According to the requirement, those two web part should work independently.

image

But if we search using the Search Box, it searches through the Search Result web part and not search the entire site. So it’ll not redirect me to the search results page as expected.

image

If we check the web part properties of the Search Box web part, we can identify the problem.

image

Currently it is set to obtain results from Search Results web part. But we need to select the other option, which is “Send queries to custom page URL”.

How we can do this programmatically?

It is very simple. We need to set the TryInplaceQuery property to false.

  1. var searchBoxScriptWebPart = new SearchBoxScriptWebPart();
  2. searchBoxScriptWebPart.TryInplaceQuery = false;

If we search using Search Box web part, now it redirects to a search result page. Now search web parts work independently as expected.

image

No comments: