Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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>
like image 760
Daoming Yang Avatar asked May 05 '09 10:05

Daoming Yang


People also ask

How do you write a CAML Query?

Select the list or library for which you want to write the query. Once you select the list, click on the New query button or Query with ViewFields button to build your CAML query.

What is a CAML Query?

CAML (Collaborative Application Markup Language) is an XML-based query language that helps you querying lists and libraries in SharePoint. This windows application has been developed to ease the work of a SharePoint 2013 and SharePoint Online developer. This tool will help you create and test your CAML Queries.

Is CAML Query case sensitive?

There is no option for case sensitivity in caml queries.


2 Answers

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.

like image 135
Tudor Olariu Avatar answered Oct 20 '22 23:10

Tudor Olariu


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;
like image 43
Sunil Kumar Pashikanti Avatar answered Oct 21 '22 00:10

Sunil Kumar Pashikanti