Monday 20 February 2012

Get Top 5 list items from Sharepoint List using CAML

Get Top 5 list items from Sharepoint List using CAML


This is a sample code for retrieving top 5 items from the SharePoint List.

SPQuery is the SharePoint class to initialize the CAML query.

This following Query will display the top 5 items OrderBy ID as Ascending is false.

SPQuery spQuery = new SPQuery();
spQuery.Query = "<Query>
   
<OrderBy>
     
<FieldRef Name='ID' Ascending='False' />
   
</OrderBy> </Query> ";
spQuery.RowLimit = 5;

4 comments:

  1. How to get particular columns using caml query

    ReplyDelete
    Replies
    1. Hi viswanath,

      In this query i am using viewfields to get the particular columns.

      SPQuery query = new SPQuery();
      query.Query = string.Concat(
      "",
      "",
      "Not Started",
      "",
      "",
      "",
      "",
      "");

      query.ViewFields = string.Concat(
      "",
      "",
      "",
      "");

      query.ViewFieldsOnly = true; // Fetch only the data that we need.

      Delete
  2. Hi why u r using ascending=false in this queries....

    ReplyDelete
  3. Hi guna,

    That is used to sort the items by ID column

    ReplyDelete