Azure / Office 365 / SharePoint Development / Search

Dynamically Changing the Result Source in a SharePoint 2013 Search Center

December 18, 2013

Almost two months ago Pascal Van Vlaenderen asked a question on twitter if there's a way to specify the Result Source in the QueryString (https://twitter.com/VanVlaenderenP/status/392920559853383680). Since that time, this question was on my to-do list to check this out.

What I proposed back then, was to change the result source in the data provider object with JavaScript. This week I had some spare time to sort this thing out.

Solution

The solution to change the result source in a SharePoint 2013 search center is to inject your own JavaScript function that will change the result source.

First thing to do is to find out the function where all the properties are set of your search results. All the magic happens in the Srch.U.fillKeywordQuery function. In that function the query text, result source ID, row limits, etx… are set.

To not completely rewrite the fillKeywordQuery function, we are going to store the reference to the original function, and call this at the end.

You don't need to write a lot of code to dynamically change the result source. What I wanted to achieve is when I used "<documents>" or "<pictures>" as a keyword in the search box, it should dynamically change the result source. My code looks like this:

This piece of code can be added in a Script Editor Web Part on the results page of your search center.

To know the IDs of your search results, you can navigate to the Site Settings > Search Result Sources (under the Site Collection Administration section) > Click on the result source > you'll find the Result Source ID in the URL.

/_layouts/15/EditResultSource.aspx?level=sitecol&sourceid=e7ec8cee-ded8-43c9-beb5-436b54b31e84&view=1

Note: at the moment this isn't working togheter with the search refiners. Right now when you're going to refine the results, the custom keyword will be removed. This is something new on my to-do list.

Result

You can check this out at: Screenr

Comments

  • Pascal Van Vlaenderen

    Nice! This opens up a lot of possibilities!

  • Pingback: View Duplicate Results in the SharePoint 2013 via JavaScript - @eliostruyf()

  • Pingback: Dynamically Changing the Result Source in a SharePoint 2013 Search Center | Xylos Blog - Applied ICT()

  • Pingback: View Duplicate Results in the SharePoint 2013 Search Center via JavaScript | Xylos Blog - Applied ICT()

  • This doesn’t work when a query is executed from outside the results page (e.g from a portal to the search center)

  • srikanth

    Great stuff, we are still waiting for the refiners to work with this :)

  • Does this work with a custom Search Results Web Part? I’ve been trying this code but it doesn’t seem to fire at all when the page loads or when I hit search. It seems as the Srch.U.fillKeywordQuery is never being called on my page, I’ve also tried using a default Search Results Web Part and still nothing happened… Is there anything I can do to modify the Result Source of the web part via JavaScript?

    Thanks,
    Santiago.

    • Santiago,

      This should work for the default search result web parts, because they all use the fillKeywordQuery. Where did you place the JavaScript code on your page?

      Regards,
      Elio

      • Hi Elio,

        I’ve placed it on a js file that I’m referencing on a Script Editor Web Part. I was now able to see that if I add a default Search Box web part the fillKeywordQuery is executing, since I’m using a custom web part for the Search Box the code is not being called.

        Regards,
        Santiago.

        • Just wanted to let you know that I found out a way to make it work with a custom search box web part, I’ll be posting what I did in my blog (which I’m creating just for this) if that’s ok with you Elio (I’d reference your posts of course). And just wanted to thank you for posting this which really helped me a lot.

        • Thomas Molbach

          I’m facing a similar challenge to what Santiago describes. As admin user, the FillKeywordQuery is firing both if I’m searching from the search home page and on the result page. If I’m a specific end user, it only works from the result page. The FillKeywordQuery code does not execute. Does anyone know why? And if there is a workaround/solution?

        • Make sure the “Search Result” web part query is set to “async” execution or the query will be performed on the server for incoming requests: Edit web part -> “Change Query” -> Settings -> “Async options”. Thomas

  • shriekz

    I have used the same code in a cross site publishing scenario. Something funny is happening. The resultSource does get attached dynamically the search is showing the filtered results, however, the URL of the result seems to be pointing to the Authoring content and not on the published content. I went to the published page to see how it was and it seemed OK, this anomaly is there only in the search page.

    Anyone else who has seen this problem? If so, any fixes, suggestions?

    • shriekz,

      This is how XSP works, all the content is on the authoring site, but it should automatically give you the correct URL if the site is linked to the authoring site.

      Are you switching the result source of multiple authoring sites? Are you referencing the correct result source IDs? Does it work if you remove the JavaScript code from the page (normally it should show the same results if you do not provide a document or picture value)?

      Regards,
      Elio

  • Hi Elio,

    I have been facing same problem what shriekz posted here. I checked the result source ids. In my case if we logged inside the server,search result URLs are pointing to publishing site however, if we open site outside the server, search results URL are pointing to authoring site.
    Could you please help me on this?

  • Erik

    Hi Elio,

    Great post! I’m trying to implement something similar to this on our public facing website. Everything works as expected when users are authenticated. However, when an anonymous user aren’t able to switch the result sources.

    Do you know of anything separate that needs to be done for this to work for anonymous users?

  • Pingback: Search JavaScript calls not working for anonymous users - @eliostruyf()

  • uday

    HI Elio, i have a content search web part and am using query which is taking value from query parameter. Now by default, we do not have query parameter so content search not displaying any result. I want to show all items in content search when there is no query parameter when query parameter specified in the URL i need to filter with that. Please suggest me what to do?

    • uday

      OR is there any way to filter that content search results based on querystring parameter?

      • You can use {?{QueryString.ParameterName}} in the query. {? … } will remove the query string parameter from the query if the value is blank or isn’t added to the URL.

        • uday

          Thank you so much Elio. its worked perfect. I have one query, is there any way to get followed documents and liked discussion in content search?

        • Documents you are following has to be retrieved from the Social API endpoint: /_api/social.following – so I think you will have to build something for this.

          Liked discussion can be retrieved via a search query, because the ratings or likes are stored on the item itself. So you can do a search on LikesCount managed property.

        • uday

          Thanks Elio for your quick response. is there any way to find liked items by logged in user?

        • The people who liked the item are stored in the LikedBy field. Check if this is available as managed property or crawled property. If it is, you can use that for your search query.

        • uday

          Hi Elio, thanks I will check it. I have one more query, is it possible to show video in content search nd make it playable with display template?

        • Yes, that is a possible. Check out the Video hoverpanel Display Template in your master page gallery: /_catalogs/masterpage/display templates/search/item_video_hoverpanel.html

        • uday

          Thank you so much Elio.

  • Dan A

    Hi Elio, thank you for another fantastic article. I was wondering though if you ever did circle back and get the refiners working?

    “Note: at the moment this isn’t working togheter with the search refiners. Right now when you’re going to refine the results, the custom keyword will be removed. This is something new on my to-do list.”

    I am trying to figure out how to get them to work again now after applying the result source, but haven’t been able to figure it out. Anyways though thanks again for a great article!

    • Hi Dan, never looked into it. Maybe a “cleaner” solution would be to redirect the user to another page with the right result source. So you could still use the same approach, but instead of setting the source ID, you redirect them. That way your refiners will work, and you can also use the refiners that matter for the corresponding result source.

  • SP Desperate

    Hi Elio, can you please tell me how can I change the search results limit dynamically using javascript ?
    I have 10 as a default but i want to change that using JavaScript on the fly.

    • You can do this as follows:

      Important: you need set both the resultsPerPage and numberOfItems.
      – resultsPerPage defines how many results there are rendered with the display templates. –
      – numberOfItems defines how many results to retrieve.