Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
1.5k views
in SharePoint Server by 29 37 42

In SharePoint 2019, I am using CAML Query in Visual Web Partt o get only the top 10 rows returned from the CAML query result.

I tried to use the below code, but it doesn't work

SPQuery.Query = @"<OrderBy><FieldRef Name='Created' Ascending='FALSE' /></OrderBy><RowLimit>1</RowLimit>";

How can I get Top 10 in CAML Query in SharePoint 2019?


1 Answer

1 like 0 dislike
by 151 169 345
selected by
 
Best answer

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 );
by 29 37 42
0 0
Thanks for posting multiple options, , This was worked from me "spQuery.RowLimit = 10;"
If you don’t ask, the answer is always NO!
...