This is going to be a rather a small post, on how to change the query template for the content search webpart and Search results dynamically through javascript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var showDocs = function() { | |
var qGroups = Srch.ScriptApplicationManager.get_current().queryGroups; | |
$.each(qGroups, function() { | |
if (this.displays != null && this.displays.length > 0) { | |
// Result source GUID | |
this.dataProvider.set_sourceID('70949b02-0f2c-4026-9c4f-c9d6e0023aa4'); | |
this.dataProvider.issueQuery(); | |
} | |
}); | |
}; |
The above code is more self explanatory, this approach works for both Content Search and Search Results WebPart. You can use like below to identify whether its a Content Search or Search Results WebPart
if (this.displays[0] instanceof Srch.ContentBySearch)
if (this.displays[0] instanceof Srch.Result)
Download the complete code snippet!
You can download the complete code from above link, which I have used here. Add it to the Content Editor/Script Editor WebPart.
Happy coding! Peace!!
Comments
Post a Comment