SharePoint CAML Query OrderBy Operation

Get TOP 10 Records depending on metadata column with CAML Query

using (SPSite site = new SPSite(SPContext.Current.Web.Url))
using (SPWeb curWeb = site.OpenWeb())
SPList oList = curWeb.Lists.TryGetList(“Hitcount”);

SPQuery query = new SPQuery();

query.Query = string.Concat(
“<FieldRef Name=’Counter’ Ascending=’False’ />”,

query.RowLimit = 10;

query.ViewFields = string.Concat(
“<FieldRef Name=’Title’ />”,
“<FieldRef Name=’Counter’ />”,
“<FieldRef Name=’URL’ />”);

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

SPListItemCollection oListItem = oList.GetItems(query);

DataTable dt = new DataTable();
dt = oListItem.GetDataTable();

GridView1.DataSource = dt;



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s