Sharepoint search box web part - results only current site

Iron Contributor

I thought this would be easy but I can't find is out.

On a site I use the (standard) search box web part. When I use this it returns results from the whole site collection. I do not want that. I only want to show results (documents) from the current site.

 

I did create a custom results page and configured it to show only documents of the current site. However I created this page on the root of the site collection as I do not want to create a specific search result page for al my subsites (can be a few 1000).

 

What is wrong with my approach?

17 Replies

@Mike Jansen,

 

I only want to show results (documents) from the current site

 

This of course isn't the curerent site of the search results web part. You actually want the search results from the current site where the search box came from.

 

If you look at the search box web part it goes to a url like this ( you can ofcourse replace this with your own!)

 

~site/_layouts/15/osssearchresults.aspx?u={contexturl}

 

so you could include path=... as an addtional query string. Then your search results web part cfould respond to that.

 

Within the web part properties you can also enable the drop down ( in the web part settings). The negative of this option is that this site isn't the default scope.

 

 

 

 

Try adding this to your Change Query option under the properties of the the Search Results Web Part

Path:{Site.URL}

Hi @Pieter Veenstra,

 

Let me see if I understand you correct.

I would need to change the webpart property to : ~site/_layouts/15/osssearchresults.aspx?u={contexturl}path=..

Can you give me some kind of example. As you understand this path= must be something link path=[some variable] to have this a dynamic solution.

 

Thanks, Mike


@Brent Ellis wrote:
Try adding this to your Change Query option under the properties of the the Search Results Web Part

Path:{Site.URL}

I tried that but it takes the url of the page where the search results page is located (the root in my case) not the page where the query was issued.

Hi @Brent Ellis,

 

Does that work?

 

Isn't Site.Url using the Site of the search results web part? Therefore If all search boxes on many sites all need to lead to the same search results page then that wouldn't work. you would get results from where the search result page has been created.

 

 


@Pieter Veenstra wrote:

Hi @Brent Ellis,

 

Does that work?

 

Isn't Site.Url using the Site of the search results web part? Therefore If all search boxes on many sites all need to lead to the same search results page then that wouldn't work. you would get results from where the search result page has been created.

 

 


Indeed my experience as well

Hi Mike,

 

You can use any page you like. Then within the web part properties of the search boxes configure the url of your custom results page.

 

The Url I gave you is the out of the box one. Whcih will not work for your requirements as you will need to update the query in the results web part.

 

You could make the query in the results web part include a querystring parameter. 

 

I would create a page (e.g. pages/results.aspx) then add the results web part on that page.

 

So in the search box I would configure a url like:

(I've not tested this)

 /anysite/Pages/results.aspx?u={contexturl}&mypath=~site

 

Then in the search results web part you will get a query like:

 

{searchboxquery}  Path:{QueryString.mypath}

 

 

 

 

 

 


@Pieter Veenstra wrote:

Hi Mike,

 

You can use any page you like. Then within the web part properties of the search boxes configure the url of your custom results page.

 

The Url I gave you is the out of the box one. Whcih will not work for your requirements as you will need to update the query in the results web part.

 

You could make the query in the results web part include a querystring parameter. 

 

I would create a page (e.g. pages/results.aspx) then add the results web part on that page.

 

So in the search box I would configure a url like:

(I've not tested this)

 /anysite/Pages/results.aspx?u={contexturl}&mypath=~site

 

Then in the search results web part you will get a query like:

 

{searchboxquery}  Path:{QueryString.mypath}

 

 

 

 

 

 


Hmmm, have to do some more testing but first test gives the url like this: .......134&mypath=~site&k=test&ql=1043

~site is not changes to the url of the site.

(fyi "test" is my search)

You might have to hard code the url in your search boxes.

Looks that way. I will try some solutions to see if I can create a more global solution.

If you are in a position where you can us PnP to provision your web parts then {site} would contain the url.

Ah thx, missed that point, ya, we have always resorted to hard-coding the Paths and -Paths, and using the QueryStrings like Peter mentioned, less trouble than its worth.

Part 2 ;)

 

Solved it but, a bit strange, I think.

Like mentioned I've changed the results page url like this:

https://mytenant/sites/test/sitePages/results_test.aspx?u={contexturl}&mypath='https://mytenant/sites/test/customer500/case1'

 

I did not do anything with the "mypath" variable anywhere. Even not in the result source.

I'm not a big fan of solutions that suddenly work, without knowing why ;)

 

Can someone explane why this seems to work?

@Mike Jansen,

 

Did you check both your result source and the web part query?

In advanced mode:

 

"path:"https://blabla.sharepoint.com/sites/test"  (FileExtension:doc OR FileExtension:docx OR FileExtension:xls OR FileExtension:xlsx OR FileExtension:ppt OR FileExtension:msg  OR FileExtension:pptx OR FileExtension:pdf) (IsDocument:"True" OR contentclass:"STS_ListItem")"

 

Nothing magic here. The result source is just the "documents" result source

So if you take:

 

"path:"https://mytenant.sharepoint.com/sites/test" 

 

Do you still get the results back for the /sites/test site only?

 

I would always configure the result source as Local sharepoint results. and then only use the query to do the filtering.

 

 

@Pieter Veenstra

 

All I did was set the result source to "documents" and change the URL of the results page and now I get results from the subsite: https://mytenant.sharepoint.com/sites/test/customer1/order2

That's all. When I check the advanced mode I see the above but I did not configure that manually.