2

I'm trying to add the searched text value to the query string in the url (<a href='...?searchText=[something]'>Title</a>)

In the search display templates, I see ctx and Srch objects exist that have a lot of properties about the current item and general search properties. However, I'm having an issue trying to get the actual searched value (in pairs or all in one string). I can extract it from the url hash since it's not in the querystring anymore, but I was wondering if there was a more standardized way to do this.

edit: for now, I have two methods to extract a querystring value and a hash value, and setting searchText (which is conditionally appended to the url) in the following way:

var searchText = "&searchText=" + GetHashVal("k") || GetQueryVal("k");
3
  • I'm not clear can you explain, why search term is not in the querystring? Commented Apr 13, 2016 at 21:42
  • SharePoint 2013 puts it in the hash if the user searches on the page. I believe their reasoning is because it lets the UI change without the page refreshing. The same happens on list views now.
    – Dinerdo
    Commented Apr 13, 2016 at 21:57
  • Maybe here some ideas: sharepoint.stackexchange.com/questions/114480/…
    – Patrick
    Commented Apr 14, 2016 at 6:28

1 Answer 1

4

You can retrieve it from the DataProvider object. Which is accessible from within your display template.

ctx.DataProvider.get_currentQueryState()

This returns an object which gives you the whole search query context, like: keyword, sorting, refinement, ...

In your case you want to extract the keyword, which you can retrieve from the property like this:

var keyword = ctx.DataProvider.get_currentQueryState().k;
2
  • I had to load sp.js with $includeScript which added the SP.ClientContext object, but DataProvider is still undefined.
    – Dinerdo
    Commented Apr 18, 2016 at 15:58
  • ctx.DataProvider should be available from inside your display template. As this is also used for rendering the results. No need to load other files. Commented Apr 25, 2016 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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