0

Does anyone know how to modify search results before they are rendered when the modification depends on data returned from an async request?

I have a requirement to filter some search results according to only show results from sites that the user is following.

So far I have done this by calling some JavaScript from my display template which iterates through the page of results and removes whichever are not amongst the users followed sites (which I have retrieved with the SocialFollowingManager).

This approach works but it does cause some problems around the results count and paging and I would much prefer to remove the unwanted result rows from the result set before they are rendered to the page.

I can do this by modifying the results in ctx.ListData.ResultTables[0].ResultRows but unfortunately by the time I have retrieved the data I need to do the filtering the page has loaded and the results have rendered.

Is there a way I can hold the rendering of the display template back while I wait for the async request to complete?

I tried using OnPreRender but even though the process was kicked off before the render began it still didn't work because the page loaded before the async call returned. I also tried requesting the data with a non-async call (just ExecuteQuery) but it seems that this isn't possible (see https://stackoverflow.com/questions/12147472/cant-get-clientcontext-exicutequery-to-work-in-javascript).

Or is there just a better way of doing this altogether?

  • Are you using SharePoint Online or SharePoint on premises? – Matthew McDermott Nov 27 '14 at 14:18
  • I'm on premises. – Tim Dec 2 '14 at 10:06
0

You can handle the OnPreRender handler. Here is a pseudo code you can try with

ctx.OnPreRender = preRenderHandler;

function preRenderHandler(ctx) {
    //Get the Page's user is following here

    //Remove the result here by accessing 
    ctx.ListData.ResultTables[0].ResultRows
}

While retrieving Followed pages data you should hold back the call by making it Synchronous using jQuery promises

http://api.jquery.com/promise/

http://api.jquery.com/jQuery.when/

http://api.jquery.com/deferred.promise/

  • 1
    I'm not clear what you are suggesting Amal. I've tried using OnPreRender but it doesn't alter the fact that the CSOM api to retrieve social data only takes asyncronous calls. I don't see how using a promise rather than a callback would change this. – Tim Dec 5 '14 at 10:28
  • @Tim I am suggesting to block the PreRender call till the SocailData is retrieved. – Amal Hashim Dec 5 '14 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.