The past few weeks I have got the same question a couple of times how to make it work for anonymous users. The problem when an anonymous user tries to switch the result source, it appears that nothing happens. The same results are returned as the original search query he performed.
The reason for this is that anonymous users need to be allowed on the site to change query parameters like the result source ID before they can perform such queries. To allow these query parameter changes for their queries, extra configuration is required on your site.
Configuration
As you may or may not know, if you want to perform anonymous REST calls to the search API you have to specify a configuration file. In this configuration file you specify the action that are allowed for anonymous users to perform. This configuration file is also required if you want to do search queries from within you display templates or JSOM as an anonymous user.
Note: More information about the configuration steps can be found on Waldek Mastykarz his blog and on MSDN.
Configuration steps
The following configuration steps are required:
Create a new document library**
**named QueryPropertiesTemplate on the site where you want to do your anonymous JS calls;
Create a new XML file named queryparametertemplate.xml, and copy the following XML content to the file:
Note: Two additional properties have been added to the WhiteList element. These properties are SourceID and Refiner. More information about these properties can be read in the last section of this article.
Update the FarmId, SiteId, and WebId elements in the XML content to that of your environment;
Upload the queryparametertemplate.xml file to the QueryPropertiesTemplate document library you created.
The last step that is explained in each of the two articles is the step where you need to add the QueryTemplatePropertiesUrl parameter and file location to your search Rest call. Of course this is not possible when you are working via JSOM.
When working with JSOM you need to specify the QueryTemplatePropertyUrl parameter on the DataProvider object. This can be performed like this:
To finish the code to switch the result source ID for anonymous users, the previous code snippet needs to get added to every call an anonymous user will perform.
<script>// Show duplicated results
if(typeofSrch.U.fillKeywordQuery!=='undefined'){// Override the fillKeywordQuery function
varoriginalFillKeywordQuery=Srch.U.fillKeywordQuery;// Override the default fillKeywordQuery function
Srch.U.fillKeywordQuery=function(query,dp){// Retrieve the current query text
varqueryText=dp.get_currentQueryState().k;dp.set_queryPropertiesTemplateUrl('spfile://webroot/queryparametertemplate.xml');// Check for your custom keywords
if(queryText.indexOf('<documents>')>=0){// Remove the custom keywords text from the search query
dp.get_currentQueryState().k=queryText.replace('<documents>','');// Set the documents result source
dp.set_sourceID('e7ec8cee-ded8-43c9-beb5-436b54b31e84');}elseif(queryText.indexOf('<pictures>')>=0){// Remove the custom keywords text from the search query
dp.get_currentQueryState().k=queryText.replace('<pictures>','');// Set the pictures result source
dp.set_sourceID('38403c8c-3975-41a8-826e-717f2d41568a');}else{// Specify an empty source ID to reset the default one
dp.set_sourceID("");}// Call the default function to go further with the query processing
originalFillKeywordQuery(query,dp);};}</script>
Note: The highlighted line is where you need to set the query template property URL.
Why are these additional WhiteList properties required
The SourceId element is required to allow anonymous users to change the result source ID for the query. If you only add this property, you get the following result:
When you only added the SourceId element, you do not get any refiners. That is why you also need to add the Refiners element to the white list. Once you have done this, you will get all the refiners where the managed properties are configured as safe.