Forum Discussion
Mike Jansen
Apr 05, 2017Iron Contributor
Call "standard" search result page (or webpart) from custom query
I have a document library with a lot of documents (+1000). The documents have a "Label" column.
I created a script which reads all those different Labels and populates a dropdown:
When I select a label and push "Search" a query is fired which gives me all documents with (in this case) the label "KG1".
What I would like to do is to send the queryresults (or the query) to a standard search result webpart and/or to a query result page. In that way the user can use the default refiners an preview possibilities.
So far, I did not find a way to achieve this.
Can someone point me into the right direction?
Thanks, Mike
- You need to construct the KQL to pass to limit for what you need:
/_layouts/15/osssearchresults.aspx?u=https://..path-to-document-library&k=myterm prop1:propvalue
If you column with a property is a site column it will have generated an automatic managed property for you which you can query for. Say you have a text site column named "Foo", then the managed property would be named FooOWSTEXT, and you query would be:
/_layouts/15/osssearchresults.aspx?u=https://..path-to-document-library&k=myterm FooOWSTEXT:label
- paulpaschaBronze Contributor
Search Results pages can be requested with several Query String Parameters. The QueryString Parameter "k" can be used to pass you query text. So I think the easiest way is to pass your query text to a Search Results page through the "k" querystring parameter.
- Mike JansenIron ContributorOk, but how do I trigger that from code? Especially using a search web part.
When I push the search button, this query is generated and fired:
https://blabla.sharepoint.com/_api/web/lists/getByTitle('Documents')/items?$select=FileLeafRef,FileRef,File_x0020_Type,Categorie,Label/Title&$expand=Label&$filter=Label/Title eq %27"+ inputLabel +"%27&rowlimit=500
Inputlabel is the result from my dropdown.
Would be something like: "Currentsite/result.aspx?k="myquery" ?
Is it also possible to stay on the current site, without a complete page refresh?- paulpaschaBronze Contributor
The value of the "k" querystring parameter is read by the Search Results Web Part. In other words, if you put a Search Results Web Part on your page you should be able to start querying by appending the k Query String parameter with your search term value to the URL and see the results. You can use JavaScript to request a specific page (or the page you're currently on) appending the required querystring parameter.