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

3

18

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>
Hanoverian answered 5/5, 2009 at 10:8 Comment(0)
H
23

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.

Hileman answered 5/5, 2009 at 11:36 Comment(2)
If I use this query - <Query><Where></Where></Query><View><RowLimit Paged='False'>10</RowLimit></View> - I'm getting an error 'There are multiple root elements'. Any ideas?Reflexive
Do not put <View> in the Query; the SPQuery object has a separate property called View which you need to use, and also a RowLimit if I remember correctlyHileman
O
10

The below code shows top 5 records from the list (by using CAML query).

SPQuery spQuery = new SPQuery();
spQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='FALSE'/></OrderBy>";
spQuery.RowLimit = 5;
Olericulture answered 9/12, 2009 at 16:12 Comment(0)
U
0

If you want to construct other simply caml queries try this tool. http://www.camldesigner.com/

P.s. tool doesn`t construct a "paginate" caml queries with SPListItemCollectionPosition.

Unmeasured answered 3/4, 2014 at 9:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.