Microsoft recently made Azure Cognitive Services available (in preview at the time of writing). This new set of APIs is built on top of Azure’s API Management building block and points to well known APIs (Machine Learning, Bing…) that Microsoft was offering for a while but in different flavors. Thanks to Azure Cognitive Services, we now have a single way of consuming these APIs which makes our life better!
The Bing AutoSuggest API can be very handy to improve any search experience. In SharePoint Online, one can use Query Suggestions but they are defined at tenant level and they need to be imported upfront. This is not that user friendly. So, what if you could simply have a Bing/Google like experience that is suggesting queries as you type? That’s what we’ll do very easily in this post.
If like me, you only believe what you see, you can have a look at a live demo here:
Note : for this example, I have not been using the SharePoint Framework (SPFx) but simply jQuery UI’s autocomplete control in combination with Azure Cognitive Services. So, here are the steps to build such a control:
- Go to https://portal.azure.com/ and look for the Cognitive Services:
- Create a new Cognitive Services Account in order to end up with something like this:
the important part is highlighted in red. I’ve chosen the Standard pricing tier (no free one) and I’ve specified the Bing Autosuggest API.
- Once done, grab one of the keys that was generated for you as you’ll need it later on:
The Azure part is done.
- Now, in a SharePoint search center, add a Snippet Editor to the default page
- In the Snippet Editor, add the code that I will show you later in this post
- Go to the webpart properties ==> advanced section, and enable the webpart to be exported as illustrated below:
- Now that the webpart can be exported, you can do so, this will generate a .webpart file:
- This webpart file may now be imported in any site collection via the webpart gallery.
The important part here is the communication with the Bing AutoSuggest API which is handled by the AJAX call. You’ll simply have to update the key value that is associated with the Ocp-Apim-Subscription-Key HTTP header. As you should have noticed yourself by now, Azure Cognitive Services are CORS compliant, which is great for scenarios like this one. However, one should keep in mind that the subscription key is revealed as anyone launching the browser dev tools can see it. Of course, there is no sensitive information here since it’s just a matter of getting query suggestions back but anyone in the world holding your key could start consuming the API with you paying for it!
As a mittigation measure, keys can be regenerated at any time from the Azure Portal. If you don’t want to take any risk, you should build your own intermediate API in Azure PaaS, protected by Azure AD and let this one use the subscription key. This is of course more complicated and will cause a performance overhead because of the extra hop. I’d say that in this particular case, since there is no sensitive information at all, it’s worthwhile to take the risk.