SharePoint: How to get Top 5 records by using CAML query from a list

Daoming Yang picture Daoming Yang · May 5, 2009 · Viewed 40.5k times · Source

I have already created a webpart to show the data from list, but I really want is to only show top 5 records from that list (by using CAML query).

Does anyone know how to do this? Many thanks.

<Query>
   <OrderBy>
      <FieldRef Name='ID' Ascending='False' />
   </OrderBy>
</Query>

Answer

Tudor Olariu picture Tudor Olariu · May 5, 2009

You could set the RowLimit property of your SPQuery object.

The <RowLimit> tag is in the schema definition of a view (direct child of <View>) and therefore cannot be nested inside a <Query> tag.