I am using Sitecore 9.3 JSS React Solution with Coveo. There is a custom computed field that contains the security assigned to the particular Sitecore page. The requirement is to match the field value with the user role value. If the match doesn't succeed then the page should be hidden from the search results.
I have been referring several javacripts to do the same referring to below links: JavascriptSearchFramework
I tried the below script:
document.addEventListener('afterInitialization', function () {
if (typeof window !== "undefined")
{
const root = document. Body;
var queryBuilder = new window["Coveo"].QueryBuilder();
root.addEventListener('buildingQuery', function(e) {
queryBuilder.advancedExpression.addFieldExpression('@securityinfo', '==', ['<roleName>'])
});
window["Coveo"].init(root);
}
});
Note: I already tried with the event "DOMContentLoaded
" instead of "afterInitialization
", but the function was not executing.
I am new in Coveo module, please guide me if I am doing anything wrong here. Thanks in advance.