That works fine in Classic, but when I looked at the URL in Lightning I noticed that it's quite different.
../one/one.app#eyJjb21wb25lbnREZWYiOiJmb3JjZVNlYXJjaDpzZWFyY2hQYWdlI...
The first thing you may notice is that our search term is nowhere to be found. That's because it has been converted to a base64 encoded string value. In order to see how to pass our search terms, we need to decode this value and see what it consists of. You can do this online at sites like https://www.base64decode.org/. Here's the decoded value.
{"componentDef":"forceSearch:searchPage","attributes":{"term":"ACME Corporation","scopeMap":{"resultsCmp":"forceSearch:resultsTopResults","label":"Top Results","type":"TOP_RESULTS","cacheable":"Y","id":"TOP_RESULTS","labelPlural":"Top Results"},"context":{"disableSpellCorrection":false,"disableIntentQuery":false,"permsAndPrefs":{"OrgPermissions.UnionAppNavSmartScope":false,"SearchUi.feedbackComponentEnabled":false,"SearchUi.searchUIPilotFeatureEnabled":false,"SearchExperience.LeftNavEnhancementEnabled":true,"Search.crossObjectsAutoSuggestEnabled":true,"Search.maskSearchInfoInLogs":false,"SearchUi.orgHasAccessToSearchTermHistory":false,"SearchResultsLVM.lvmEnabledForSearchResultsOn":true,"SearchUi.searchUIInteractionLoggingEnabled":false,"MySearch.userCanHaveMySearchBestResult":false,"SearchResultsLVM.lvmEnabledForTopResults":false,"MySearch.userCanHaveMySearch":false}},"groupId":"DEFAULT"},"state":{}}
Did you notice the bold 'term' attribute? That's where we want to put our search terms. Now that we know what the encoded string consists of and where to put our search terms all that's left to do is write some code. Let's take a look at how we can take this decoded JSON and use it in a Lightning Component.
In the first line of the code, we will declare our search term variable and assign the value 'ACME Corporation'.
var searchterm='ACME Corporation';
Next, we need to take the decoded JSON and add our search term.var stringToEncode = '{"componentDef":"forceSearch:search","attributes":{"term":"'+searchterm+'"...
Now that we have the JSON ready, we need to Base64 encode it for use in the URL. For this example I created a variable that references the base64min.js library. In a real-world project, you may want to create a static resource for this.
var Base64={_keyStr:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234...
var searchcriteria = Base64.encode(stringToEncode);
And last but not least we redirect to the newly created search URL.
window.location.href='/one/one.app?source=alohaHeader#'+searchcriteria;
That's pretty much it! Here's the complete code snippet.