Oct 19 2017 12:09 AM
I have to (jquery) rest searches. The first one:
jquery.ajax({ url: `${this.props.siteurl}/_api/web/lists/getbytitle('EmployeeList')/items`, type: "GET", headers:{'Accept': 'application/json; odata=verbose;'}, success: function(resultData) {...... bla bla
This one gives a nice clean/small json results. I get my field using "resultData.d.results"
My second query is this:
jquery.ajax({ url: "https://Blabla.sharepoint.com/sites/ilss/_api/search/query?querytext='ContentType:test_matters'&selectproperties='RefinableString10%2cCreatedBy%2cCreated'&rowlimit=500'", type: "GET", headers:{'Accept': 'application/json; odata=verbose;'}, success: function(resultData) {..... blabla
This one gives me a huge json result with too much data. I have to get my data using this:
Oct 23 2017 02:06 PM - edited Oct 23 2017 02:26 PM
SolutionHey 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
Oct 30 2017 06:24 AM
@Andrew KoltyakovThanks!
Oct 23 2017 02:06 PM - edited Oct 23 2017 02:26 PM
SolutionHey 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