Friday, July 24, 2015

How to append the current site path to your search query

The situation: I had to add product and people search blocks to the search experience in my site collection.  Since the osssearchresults.aspx page cannot be edited, I created a new search results page.  I set the page query to only return results from my site collection. Once I had everything configured, a change request came in to only have results returned from the hierarchy where the search originated.

The problem: Since my search results page sat at the top of the site collection, I needed some way to pass the path where the search was performed to the results page.  I noticed that there was a parameter of u=[path] that seemed to appear frequently as I was navigating around my SharePoint sites, but I couldn't figure out how to append that to my URL as the search was sent.  I tried /.../Pages/Search-Results.aspx&u={lots_of_wrong_tokens}, but couldn't get anything to convert into the actual path.

The solution: After spending a lot of time researching with no luck, I noticed while editing the Search Box web part that there was an option to direct the queries to a Results Page URL of "~site/_layouts/15/osssearchresults.aspx?u={contexturl}" instead of sending to the search results web part on the page.  I appended "?u={contexturl} to the end of my search results page path on the Search Settings page, and like magic, that token converted into the path where the search originated.

I modified my Search Results web part query to "{searchboxquery} Path:{QueryString.u}", and my results page was only returning results from the hierarchy where the search was performed.

Possible issues: Visiting the search results page without a query used to return all results; now it shows no matches.  This isn't a huge deal, but not having the u=[path] in the URL leads to an empty search, and if there is a keyword parameter but no path, you see ye olde "something went wrong" message.



My failed searches: "send queries to custom results page URL", "sharepoint 2013 append something to search query", "sharepoint 2013 how to append something to the search query automatically", "sharepoint 2013 master page append text to search query" (yes, I was getting desperate!), "sharepoint 2013 using a custom search results page", "SharePoint 2013 how to edit osssearchresults.aspx", "SearchSmallInputBox", "add source to SharePoint 2013 search", "append path to search query URL", "SharePoint 2013 search default page add u=", "sharepoint 2013 search only this site", "SharePoint append path to search string", "sharepoint add path property to URL"

1 comment:

  1. Thx Lisa, also works with the drop down list inside the search box. I wanted to remove the people and conversation element. But still have the possibility to search the current site or the whole farm. With your tip it works. For everyone who is lookiing for it just use "/_layouts/15/osssearchresults.aspx?u={contexturl}".
    It is not working if instead of {contexturl} you take the url or encoded url!

    ReplyDelete