Forum Discussion

Mike Jansen's avatar
Mike Jansen
Iron Contributor
Jan 10, 2017

Sharepoint search box web part - results only current site

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

  • Brent Ellis's avatar
    Brent Ellis
    Silver Contributor
    Try adding this to your Change Query option under the properties of the the Search Results Web Part

    Path:{Site.URL}
    • Pieter Veenstra's avatar
      Pieter Veenstra
      MVP

      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.

       

       

      • Brent Ellis's avatar
        Brent Ellis
        Silver Contributor
        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.

    • Mike Jansen's avatar
      Mike Jansen
      Iron Contributor

      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.

  • 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.

     

     

     

     

    • Mike Jansen's avatar
      Mike Jansen
      Iron Contributor

      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

      • Pieter Veenstra's avatar
        Pieter Veenstra
        MVP

        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}

         

         

         

         

         

         

Resources