We have built a custom knowledge base for our client. We have search enabled and users can search for KB articles. Client wants us to pass search query which lead user to that particular article.

How can I pass query which was performed to item which user clicked in search results page?


Here's a couple of quick ways to pass the query text to the result page:

  1. Append the query text to each search result hyperlink url as a query string parameter
  2. Add an onclick event handler to each search result hyperlink and save the query text to a cookie

You can accomplish each of the above by using jQuery or by editing the search results web part XSLT.

  • Could you provide some materials on editing results web part and how can I access query from there? – jjczopek Oct 13 '12 at 19:00
  • You can edit the XSLT for the search results web part by going to the web part properties, unchecking the "Use Location Visualization" checkbox under the "Display Properties" section and then clicking the "XSL Editor..." button. The variable that holds the query text in the XSL is "$Keyword". Using jQuery may be easier though - simply grab the query text from the "k" query string or from the search box on page load and then append it to the url of each search result hyperlink. You can embed the javascript right into the page by using a Content Editor Web Part. – Vassili Altynikov Oct 13 '12 at 20:45

If you're using standard SharePoint search pages, then you'll automatically get that passed to your KB article page.

HttpContext.Current.Request.UrlReferrer contains the full URL of the page which referred to the current one including QueryString.

So in a control on the KB article page you'll be able to check the UrlReferrer to check if it was a search result page and in that case the k QueryString parameter will be the search query

  • KB articles are as documents in pages library, so I can't get handle of HttpContext. – jjczopek Oct 14 '12 at 7:15
  • Why can't you put a control on these pages? If it's your own page layout, then you can add a control to that. If it's your own master page, then you can add a control to that. Or you can add a control using a Delegate control. And finally you have the option of using a http module. If you can't add code anywhere then you can't solve the problem. – Per Jakobsen Oct 14 '12 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.