Export SharePoint Search Content to CSV
This came as a client requirement and made me scratch my head for days. The requirement was that the client would perform a search, apply refiners, change sort order etc. and we needed to export the result that is being displayed to the user. In rest api, we have an end point for search which accepts QueryText and Refiners and returns search result which we can write to the CSV file.
So we will be converting
https://*****.sharepoint.com/sites/Search/Pages/search.aspx?k=test#Default={"k":"test","r":[{"n":"RefinableString15","t":["\"ǂǂ5265702043617020506c616e6e657220616e64204461746162617365\"","\"ǂǂ50726f706f73616c205265636f726473\""],"o":"OR","k":false,"m":{"\"ǂǂ5265702043617020506c616e6e657220616e64204461746162617365\"":"Some Text","\"ǂǂ50726f706f73616c205265636f726473\"":"Some Other Text"}}],"l":1033}
to,
https://******.sharepoint.com/sites/Search/_api/search/query?querytext='test'&refiners='RefinableString15'&refinementfilters='(OR(RefinableString15:("Some Text"),RefinableString15:("Some Other Text")))'&rowlimit=500&startrow=0&trimduplicates=false
First we will extract hash from the current url,
var hash = window.location.hash;
There is a search keyword type in and refiners are applied
//specifying search keyword
But the real challenge is to transform all the operations that are done by the user on the search page to the acceptable parameters of rest api. As you might have noticed when you perform the search, whatever operation you perform, the parameters are added to the query string and a request is sent to the server. So we can read the parameters from the query string and create rest api specific parameters.
So we will be converting
https://*****.sharepoint.com/sites/Search/Pages/search.aspx?k=test#Default={"k":"test","r":[{"n":"RefinableString15","t":["\"ǂǂ5265702043617020506c616e6e657220616e64204461746162617365\"","\"ǂǂ50726f706f73616c205265636f726473\""],"o":"OR","k":false,"m":{"\"ǂǂ5265702043617020506c616e6e657220616e64204461746162617365\"":"Some Text","\"ǂǂ50726f706f73616c205265636f726473\"":"Some Other Text"}}],"l":1033}
to,
https://******.sharepoint.com/sites/Search/_api/search/query?querytext='test'&refiners='RefinableString15'&refinementfilters='(OR(RefinableString15:("Some Text"),RefinableString15:("Some Other Text")))'&rowlimit=500&startrow=0&trimduplicates=false
First we will extract hash from the current url,
var hash = window.location.hash;
var groups= hash.split("#");
Whatever user inputs in the search box is set to parameter named K in search url. If we only have a search keyword and no additional refiners are applied
if(groups.length == 1 && groups[0] == ""){
if(getURLParameter(fullUrl,'k'))
{
var encodedValue = getURLParameter(fullUrl,'k');
var jsonStringValue = decodeURIComponent(encodedValue);
var safejsonStringValue = jsonStringValue.replace(/'/g,"''");
refinerUrl +="&querytext='"+safejsonStringValue+"'";
}
}
There is a search keyword type in and refiners are applied
else{
//if format is #k=bolivia or k=bolivia#Default=..
for (var i = 1; i < queryGroups.length; i++) {
if (queryGroups[i].length > 0) {
var keyValue = queryGroups[i].split("=", 2);
var key = keyValue[0];
var encodedValue = keyValue[1];
if (key === "Default") { // json string format
var jsonStringValue = decodeURIComponent(encodedValue);
var safejsonStringValue = jsonStringValue.replace(/'/g,"''");
var queryObject = JSON.parse(safejsonStringValue);
//specifying search keyword
refinerUrl+="&querytext='"+queryObject.k+"'";
//handle the ordering
if(queryObject.o && queryObject.o.length > 0){
//handle the ordering
if(queryObject.o && queryObject.o.length > 0){
var direction="";
if(queryObject.o[0].d == 0){
direction="ascending";
}else{
direction="descending";
}
refinerUrl+="&sortlist='"+queryObject.o[0].p+":"+direction+"'";
}
//construct refiner string
// n refers to Refiner name and m refers to value
var refiners =[]; var refinementfilters=[];
// n refers to Refiner name and m refers to value
var refiners =[]; var refinementfilters=[];
for (var i = 0; i < queryObject.r.length; i++) {
var keywordkey = queryObject.r[i].n;
refinementfilter=[];
if (queryObject.r[i].m != null) { // check if 'm' contains data
if(refiners.indexOf(keywordkey) == -1)
refiners.push(keywordkey);
for (var n = 0; n < queryObject.r[i].t.length; n++) {
var keywordvalue = encodeURIComponent(queryObject.r[i].m[queryObject.r[i].t[n]]);
refinementfilter.push(keywordkey+":("+"\""+keywordvalue+"\")");
}
}
else {
for (var n = 0; n < queryObject.r[i].t.length; n++) {
var tvalue = queryObject.r[i].t[n];
if(refiners.indexOf(keywordkey) == -1)
refiners.push(keywordkey);
if (tvalue.indexOf('ǂ') !== -1) {
// value is HEX type
var keywordvalue = hex2a(tvalue);
var keywordvalue = hex2a(tvalue);
refinementfilter.push(keywordkey+":("+"\""+encodeURIComponent(keywordvalue)+"\")");
}
//range filter
}
//range filter
else if (tvalue.indexOf('range(') !== -1) {
refinementfilter.push(keywordkey+":("+tvalue+")");
}
else { // simple format
refinementfilter.push(keywordkey+":("+encodeURIComponent(tvalue)+")");
}
}
}
if(refinementfilter.length > 1)
refinementfilters.push(queryObject.r[i].o +"(" + refinementfilter.join(',') + ")");
else
refinementfilters.push("(" + refinementfilter.join(',') + ")");
}
if(refiners.length > 0){
refinerUrl+="&refiners='"+refiners.join(',')+"'";
if(refinementfilters.length > 1){
refinerUrl+="&refinementfilters='and("+refinementfilters.join(',')+")'";
}
else{
refinerUrl+="&refinementfilters='("+refinementfilters.join(',')+")'";
}
}
}
}
else if (key === "k") { // simple format
var jsonStringValue = decodeURIComponent(encodedValue);
var safejsonStringValue = jsonStringValue.replace(/'/g,"''");
refinerUrl +="&querytext='"+safejsonStringValue+"'";
}
}
}
Through these operation we will be able to build a rest api request, and then exporting the data to csv is straight forward.
The blog that helped me a lot to figure out the solution
https://splucy.wordpress.com/2016/12/31/get-sharepoint-search-parameters-from-url-hash/
}
Through these operation we will be able to build a rest api request, and then exporting the data to csv is straight forward.
The blog that helped me a lot to figure out the solution
https://splucy.wordpress.com/2016/12/31/get-sharepoint-search-parameters-from-url-hash/
Comments
Post a Comment