Search JavaScript calls not working for anonymous users and how to fix it

May 21, 2015

Back in December 2013 I wrote an article in which I explained how you could dynamically switch the result source with a search query keyword.

Note: You can find the article over here - Dynamically changing the result source in a SharePoint 2013 search center.

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.


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.
QueryPropertiesTemplate document library

QueryPropertiesTemplate document library

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.

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:

A anonymous search call with SourceId on the WhiteList

A anonymous search call with SourceId on the WhiteList

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.

A anonymous search call with SourceId and Refiners on the WhiteList

A anonymous search call with SourceId and Refiners on the WhiteList