How to Get TOP N in CAML Query in SharePoint 2019?
Instead of using <RowLimit>
, I would suggest use spQuery.RowLimit
as below
SPQuery spQuery = new SPQuery();
spQuery.Query = "<OrderBy><FieldRef Name='Created' Ascending='FALSE' /></OrderBy>";
spQuery.RowLimit = 10;
In case, you need to use <RowLimit>
, you have to ensure that the <View>
tag is used as below
SPList spList = spWebsite.Lists["List_Name"];
SPQuery spQuery = new SPQuery();
spQuery.ViewXml = "<View><Query><OrderBy><FieldRef Name='Created' Ascending='FALSE' /></OrderBy><RowLimit>10</RowLimit></View>";
SPListItemCollection collListItems = spList.GetItems(spQuery );