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
2.1k views
in SharePoint Server by 30 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 159 186 375
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 30 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!
...