Forum Discussion

Sumit Garg's avatar
Sumit Garg
Copper Contributor
May 09, 2017

Content Search WebPart

Hi All

I need to execute a custom JavaScript function once the display template of Content Search Webpart is rendered so I am calling that method inside "AddPostRenderCallback", my custom JavaScript is executing a method (customCSWPContext.get_dataProvider().issueQuery();) which reloads the webpart and hence it is getting stuck in infinite loop as custom function call inside "AddPostRenderCallback" will be executed again as webpart is reloaded by "issueQuery" method.
Is there any other way to execute a custom javascript after display template of CSWP is rendered? Any help would be highly appreciated.

 

Regards,

Sumit Garg

  • 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.

    • Sumit Garg's avatar
      Sumit Garg
      Copper Contributor

      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 Svenson's avatar
        Mikael Svenson
        Steel 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.

Resources