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(
“<OrderBy>”,
“<FieldRef Name=’Counter’ Ascending=’False’ />”,
“</OrderBy>”);

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;
GridView1.DataBind();

}
}

Advertisement

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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