Development

Search Using the Result Source Name with O365 Search REST API

When our team performs code refactoring on custom web parts, the following two objectives are identified with the team:

  1. Apply REST API instead of JavaScript CSOM to get better performance.
  2. Remove as many configurations as possible.

There are lots of web parts retrieving data via the search query (CSOM) and each of them contains a result source id configuration item.

For example, the following screenshot shows the typical JavaScript CSOM code that executes a search query. The highlighted line contains the hard-coded results source id value for “Local SharePoint Results”.

How to Search Using the Result Source Name with O365 Search REST API

Figure A. Typical JavaScript CSOM code that executes a search query

Unlike the built-in “Local SharePoint Results” result source, the custom result sources have different result source id values for each site collection. It requires a configuration file for each site collection that keeps track of the result source id value for each result source.

The following two screenshots show the different result source id values for the same custom result source “Featured Content”.

How to Search Using the Result Source Name with O365 Search REST API

Covid 19
COVID-19: Digital Insights For Enterprise Action

Access Perficient’s latest insights into how you can leverage digital technologies to not only respond to the pandemic, but drive your operations forward and deliver experiences your customers need.

Get Informed

Figure B. Result source id value for “Featured Content” in site collection A.

How to Search Using the Result Source Name with O365 Search REST API

Figure C. Result source id value for “Featured Content” in site collection B.

The solution to this problem is to use the Result Source Name instead of the Result Source Id in the query.

Two query properties are required: “SourceName” and “SourceLevel”

  • SourceName is the result source display name. For example, “Featured Content” shown in Figure B and C.
  • SourceLevel indicates which level the result source been defined. There are 4 levels :

https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.search.administration.searchobjectlevel.aspx

How to Search Using the Result Source Name with O365 Search REST API

The JavaScript CSOM code can be converted to the following code. (Highlighted in the red rectangle)

How to Search Using the Result Source Name with O365 Search REST API

To query with O365 Search REST API, the following is the URL format of your search query URL.  Instead of using a hard-coded sourceid value, you can replace it with the result source name and level information.

  • REST API Search Query with Source Name:

https://site/_api/search/query?querytext=’*’&rowlimit=10&selectproperties=’Title,Path,LastModifiedTime’&properties=‘SourceName:Featured Content,SourceLevel:SPSite’

  • REST API Search Query with SourceId:

https://site/_api/search/query?querytext=’*’&rowlimit=10&selectproperties=’Title,Path,LastModifiedTime’&sourceid=’8413cd40-2156-4e00-b54d-11efd9xxxxxxxxx

This solution helped our team transform all the search queries in our legacy code with result source names. The configurations of result source GUID per site collection is no longer needed in our new code base.

About the Author

More from this Author

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up
Categories