Tuesday, January 21, 2014

How to get the Query String value in SharePoint 2013 Search Results page

Hi All,

I was working to customize the search results web part by customizing the Search Result Display Item template and the Item Hover Panel as well.
If you want to read more about how to customize the search display templates read below blog post:
http://moustafa-arafa.blogspot.com/2014/01/customize-sharepoint-2013-search.html

I was trying to get a value of the query string that SharePoint 2013 passes once you hit enter in the Search Box, and since this control doesn't have an ID, I decided to pull the value from the query string that has a key "K".

After i developed this scenario, I found that SharePoint if you ran another search keyword for the second time, It would append the newer keyword with anchor (#) and will keep the old search keyword in the query string with key "K"

For example, When you run the search for the first time with keyword "copyright":

http://myPortal2013/sites/sp2013/search/pages/results.aspx?k=copyright

If you changed the search keyword to "digital", here is the new url would be:

http://myPortal2013/sites/sp2013/search/pages/results.aspx?k=copyright#digital

In my case, i want to get the "digital" and not "copyright" keyword so i want to parse the Url and get the latest search keyword. Here is the code to accomplish this:


This code i wrote it in the outer div element of the body of your display template html file.

Note: You can use this variable "sParam" as you like in the Display Template html file.

Hope this helps.

-- ME
Post a Comment