Forum Discussion
SharePoint search query > Json gives al lot of unnecessary data
- Oct 23, 2017
Hey Mike,
In your first REST request, you're getting data from the list. The second one is a search API call.
The search data is as it is. It's absolutely normal thing. Nothing to do with it. But resulted data can be reduced to more friendly format on the client side with something like:
var endpoint = _spPageContextInfo.webServerRelativeUrl + "/_api/search/query?querytext='*'"; $.ajax({ url: endpoint, type: "GET", headers: { 'Accept': 'application/json; odata=verbose;' }, success: function(response) { var results = response.d.query.PrimaryQueryResult .RelevantResults.Table.Rows.results .map(function(row) { return row.Cells.results.reduce(function(res, cell) { res[cell.Key] = cell.Value; return res; }, {}); }); console.log(results); } });Two additional pieces of advice:
- There are `nometadata` and `minimalmetadata` OData modes which can reduce response package;
- `nometadata` and `minimalmetadata` are available in SP 2016 and SPO (2013 requires some installation on server side);
- The output format is different for different modes, this should be taken into consideration.
- Helper libraries like PnP JS Core can work with search too, it's easier to code with such libs, also, no jQuery needed for ajax calls in this cases.
Cheers,
Andrew
Hey Mike,
In your first REST request, you're getting data from the list. The second one is a search API call.
The search data is as it is. It's absolutely normal thing. Nothing to do with it. But resulted data can be reduced to more friendly format on the client side with something like:
var endpoint = _spPageContextInfo.webServerRelativeUrl + "/_api/search/query?querytext='*'";
$.ajax({
url: endpoint,
type: "GET",
headers: {
'Accept': 'application/json; odata=verbose;'
},
success: function(response) {
var results = response.d.query.PrimaryQueryResult
.RelevantResults.Table.Rows.results
.map(function(row) {
return row.Cells.results.reduce(function(res, cell) {
res[cell.Key] = cell.Value;
return res;
}, {});
});
console.log(results);
}
});Two additional pieces of advice:
- There are `nometadata` and `minimalmetadata` OData modes which can reduce response package;
- `nometadata` and `minimalmetadata` are available in SP 2016 and SPO (2013 requires some installation on server side);
- The output format is different for different modes, this should be taken into consideration.
- Helper libraries like PnP JS Core can work with search too, it's easier to code with such libs, also, no jQuery needed for ajax calls in this cases.
Cheers,
Andrew
- Mike JansenOct 30, 2017Iron Contributor
Andrew KoltyakovThanks!