Forum Discussion
Content Search WebPart
Can you explain the scenario why you are re-issuing the query inside the display template?
If you explain the scenario I'm sure there is a solution.
Hi Mikael
I need to dynamically filter results of CSWP based on a value of user profile property of current logged in user and on my page I have multiple CSWP's so to ensure that I change query only of a particular webpart I am using $getClientControl($("#<element ID>")[0]) to get CSWP object and to ensure that display template element is rendered before calling "getClientControl" I am calling my custom method inside AddPostRenderCallback.
- Mikael SvensonMay 10, 2017Iron Contributor
Sumit Garg wrote:Hi Mikael
I need to dynamically filter results of CSWP based on a value of user profile property of current logged in user and on my page I have multiple CSWP's so to ensure that I change query only of a particular webpart I am using $getClientControl($("#<element ID>")[0]) to get CSWP object and to ensure that display template element is rendered before calling "getClientControl" I am calling my custom method inside AddPostRenderCallback.
Can't you for that one webpart use query variables in the query template to get the user property you need?
someproperty:{User.InternalPropName}
See http://www.techmikael.com/2014/05/s15e03-query-variables-constant-trouble.html for more information on this.
And it's better to put postrender in the control template, and not the item template, if that's what is needed.
- Sumit GargMay 11, 2017Copper Contributor
I can't directly use query variables as the user profile property values needs to be processed(trimmed) before it can be used in query filters/variables and I have used PostRender in control template only.
- Mikael SvensonMay 11, 2017Iron ContributorI suggest you set the webpart in question to run async, and take a look at this project: https://github.com/SPCSR/HelperFunctions/tree/master/SPO-Search-Improvements
Basically you can run arbitrary commands before the wp is executed - passing in your own custom query variables or modifying the query template for the WP before being sent over. Take a look at the loadUserVariables() function in particular.
- May 11, 2017
Sumit Garg, further to Mikael Svenson's comment. If you remove items within the item template you will find that features like paging will get a bit confused as the items still count in the page size count. So the Control template is the better option.