Tuesday, January 27, 2015

How to query using the Result Source name instead of using the id

I’m not going to take credit for this as the solution was originally blogged about at http://blog.voyta.net/2014/04/26/how-to-specify-results-source-name-for-search-query-using-sharepoint-2013-javascript-client-object-model/

I’ve known for some time that you can use the result source name in the .webpart config files as previously blogged. I just didn’t connect this over to SSOM/CSOM/REST for some reason. And turns out a colleague of mine stumbled upon the post referenced at the top for a solution without notifying me :).

There are two parts needed to get this working. Add a SourceName and a SourceLevel parameter. SourceName is obvious and takes the display name you have given your result source. SourceLevel takes any of the values below and has to match the level where you created your result source.
  • SSA
  • SPSiteSubscription
  • SPSite
  • SPWeb
Result sources created on the tenant level in SharePoint Online will use SPSiteSubscription.
The original post on this show JSOM code which is easily portable to CSOM.

var clientContext = new SP.ClientContext.get_current();
var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(clientContext);

// set Source Name
keywordQuery.get_properties().set_item('SourceName', 'Test Global');
// set Source Level
keywordQuery.get_properties().set_item('SourceLevel', 'SPSiteSubscription');

var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(clientContext);
var searchResults = searchExecutor.executeQuery(keywordQuery);

clientContext.executeQueryAsync(function onSuccess() {
    // process searchResults
    // e.g. iterate through searchResults.get_value().ResultTables[0].ResultRows
}, function onFailure(args) {
    // process failure

If you however want to search in a RESTful manner, format your query by adding the source information needed to the properties property:

https://server/site/_api/search/query?querytext='*'&properties='SourceName:Your Source Name,SourceLevel:SPSite'

This beats using the &sourceid parameter with a guid any day, and allows you to move your search solutions across environments without a concern for the guid per environment.


  1. I have two domain. A and B

    From domain 'A' I login in the system. my sharepoint is running on Domain 'B'. we have one file share which is in domain 'B'

    Now I crawled all content of file on domain 'B' and it is running fine. There is single sign on implmented between domain 'A' and 'B'. Means user login on his laptop using 'A' domain and no need to login on sharepoint site. Now when user login using 'A' account and open sharepoint search page then user cant see the file share result while sharepoint list/library searched data are visible but no file share content. But when user login on sharepoint site using his Domain'B' account he can easily see the File Share result.

    Please suggest what could be the possible solution to provide file share search result on the page which will be visible for the user in both case of login '(login through domain 'A' and login through domain 'B')

    1. How are security set up trust wise? You could use a custom security trimmer to map from A to B.

  2. Hi Mikael

    I have tried to use your trick with &properties='SourceName:Your Source Name,SourceLevel:SPSite' in my REST call but it returns an error.

    this is my call

    Adresser is a Result Source defined in the site collection https://dev-intranet.nykredit.dk/search

    this is the error I get Ugyldig parameter: SortList.

    which translate to something like Invalid parameter: SortList.

    Am I missing something?

    /John Legaard

    1. Tested now, and works just fine.
      http://server/sites/mysite/_api/search/query?querytext='*'&clienttype='ContentSearchRegular'&properties='SourceLevel:SPSite,SourceName:My Site Source Name'

      If you have SPSite, be sure the result source is indeed created on the /search site collection you are targeting.

  3. Hi,

    How to switch the result source dynamically when it meets some criteria.


  4. Can I ask what file you add this code in? Is it in the webpart file or the search results display template or is this a part of a full custom solution that is used instead of the out of the box solution? Thanks!

    1. Hi,
      You can follow my referenced post on how to use sourcename for a webpart, where you basically change the webpart configuration. This post is about how you use it with API calls.