Forum Discussion
SharePoint Search results to CSV
I have a use case where I want end-users to be able to use SharePoint Search to perform searches and then export those search results to a CSV file. I found many examples online explaining how to translate the criteria the user enters into the Search Search textbox into an API request. My problem, however, is that those examples often fail when the user clicks one or more of the left-hand refiners because the new resultset is often a combination of the user's querytext and the left-hand refiners they select.
The UX I am hoping for is:
- User goes to the SharePoint Search page.
- Performs a search.
- Refines the results using the left-hand refiners.
- Clicks "Export results to Excel".
I am a software developer, so I can develop almost anything we need (i.e. embed javascript on the page, build proxy APIs, etc.). I just need some advice on capturing the querytext AND refiners the end-user selected on the SharePoint Search page so I can replay that same query to the SharePoint Search APIs to eventually generate the CSV extract I am looking for.
So far, I've tried capturing the query from the URL.
https://sharepoint2019.domain.local/search/Pages/results.aspx?k=smith&ql=1033#Default=%7B%22k%22%3A%22smith%22%2C%22r%22%3A%5B%7B%22n%22%3A%22FileType%22%2C%22t%22%3A%5B%22equals(%5C%22odp%5C%22)%22%2C%22equals(%5C%22ppt%5C%22)%22%2C%22equals(%5C%22pptm%5C%22)%22%2C%22equals(%5C%22pptx%5C%22)%22%2C%22equals(%5C%22potm%5C%22)%22%2C%22equals(%5C%22potx%5C%22)%22%2C%22equals(%5C%22ppam%5C%22)%22%2C%22equals(%5C%22ppsm%5C%22)%22%2C%22equals(%5C%22ppsx%5C%22)%22%5D%2C%22o%22%3A%22or%22%2C%22k%22%3Afalse%2C%22m%22%3Anull%7D%5D%2C%22l%22%3A1033%7D
When I decode Default below is what I get, which appears to have the user's text input and the refiners they've selected:
{ "k": "smith", "r": [ { "n": "FileType", "t": [ "equals(\"odp\")", "equals(\"ppt\")", "equals(\"pptm\")", "equals(\"pptx\")", "equals(\"potm\")", "equals(\"potx\")", "equals(\"ppam\")", "equals(\"ppsm\")", "equals(\"ppsx\")" ], "o": "or", "k": false, "m": null } ], "l": 1033 }
My problem is that I can't find a way or API to which I can send this payload that will perform the same search against the SharePoint Search APIs (which I intend to use in a proxy API to generate the CSV in the desired format).
If I am going about this wrong, I'm happy to scrap this approach and perform the search another way. The important thing, however, is that the user needs to perform the search via the SharePoint Search page. They must also be able to use the left-hand refiners to refine their results further.
Any ideas on achieving the desired outcome would be much appreciated.
This is for a SharePoint 2019 on-premise installation.