1

I have a list which is to be sorted accoring to the average rating column and the top 3 items is to be displayed in react js.

This is the api endpoint used :

${this.props.siteurl}/_api/web/lists/getbytitle('Site%20Pages')/items

How to sort the result according to the average rating column to get the 3 top rated items ?

0
2

The average rating of a column is stored in a list column whose internal name is AverageRating. However, to sort on that column we need to make a POST request with a CAML query as it can't be ordered by in a normal HTTP GET request.

Make the changes from the below sample code in your spHttpClient's methods :

const options: ISPHttpClientOptions = {
    headers: {'odata-version':'3.0'},
    body: `{'query': {
      '__metadata': {'type': 'SP.CamlQuery'},
      'ViewXml': '<View>
                    <Query>
                      <OrderBy><FieldRef Name="AverageRating"" Ascending=""False"" /></OrderBy>
                    </Query>
                    <RowLimit>3</RowLimit>
                  </View>'
    }}`
  };

var endPoint = `${this.props.siteurl}/_api/web/lists/GetByTitle('Site Pages')/GetItems`;

this.props.spHttpClient.post(endPoint,
    SPHttpClient.configurations.v1,
    options)
    .then((response: SPHttpClientResponse) => {
      console.log(response.json());
      return response.json();
  });
1
  • hey @swetanayak, hope this answers your question. Please mark it as answer if it helped your query, its just a checkmark besides the answer ! It will remove this from the unanswered section as well. Thanks ! Jul 5 '18 at 15:54

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.