Question

Take this simple SPQuery for example:

SPQuery query = new SPQuery();
query.Query = "<Where><IsNotNull><FieldRef Name='Modified' /></IsNotNull></Where>";
query.RowLimit = 10;

Notice that "OrderBy" is not defined in the query.

In this case, how are the results of the SPQuery sorted before the top 10 records are selected to be returned?

Will the above query return 10 random items since no "OrderBy" is defined?


EDIT: The reason for this question was a mistake I made where I performed sorting on the query results instead of using "OrderBy" in the query. After setting RowLimit, I could not get the most recently modified items from a list.

The lesson here is to use "OrderBy" instead of sorting the results returned by the query when RowLimit is set.

Was it helpful?

Solution

If you don't specify the Order by in "SPQuery" as mention above the data return will Order by ID/Id of item of the list in Ascending order.

For Quick result you can check with rest api on browser as below:

siteURL/_api/web/lists/getByTitle('ListName')/items?$Top=5
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top