Monday, January 12, 2015

How-to: Provision search web parts configured to custom result source’s

image
When creating search solutions it’s best practice to format the query needed for a search web part via a Result Source, and not directly in the web part itself. This approach however requires you to be aware of a small quirk when moving the configuration from one environment to another.

image

The result source’s name and id chosen for the web part via the query builder is persisted back into the web part properties. Looking inside an exported .webpart file for a search result web part you find the values hidden within the DataProviderJSON property. In this case a unique GUID and the name Puzzlepart Goodies.

image

Let’s say you export the search configuration file with the Puzzlepart Goodies result source as well as the .webpart file from your development environment and upload them into staging. What will happen is that the id of the result source will change on import resulting in a random GUID. The web part will not find the result source as the id has changed resulting in showing results from the default result source instead.

The fix however is easy, replace the id of the result source with –1. This way the web part will find the result source based on the name instead of id, and you get what you expect!

image