I have a news list with ID, Title, Page Image, Page Content, News Published and News Category as column names.

I would like to use a Ajax Tab Control (HTML already i have) , how can i get the data from the list (only 3 items) Most recent 1 item from each category and render in html code using Repeater.

Please help me.


You should select top 1 (RowLimit=1) most recent (OrderBy="News Published" desc) item 3 times (for each category). This makes 3 queries, but they should be quite fast anyway.

In code it will look something like this (not tested):

var camlQueryTemplate = 
    <FieldRef Name=""News_x0020_Category"" />
    <Value Type=""Text"">{0}</Value>
  <FieldRef Name=""News_x0020_Published"" Ascending=""FALSE"" />

var list = SPContext.Current.Web.GetList("/url/of/your/list");
var category1MostRecentItem = list.GetItems(new SPQuery() {
    Query = string.Format(camlQueryTemplate, "Category1"),
    RowLimit = 1
var category2MostRecentItem = list.GetItems(new SPQuery() {
    Query = string.Format(camlQueryTemplate, "Category2"),
    RowLimit = 1
var category3MostRecentItem = list.GetItems(new SPQuery() {
    Query = string.Format(camlQueryTemplate, "Category3"),
    RowLimit = 1

// here you might also want to categories

MyRepeater.DataSource = new [] { 

Here I'm assuming that you created the list and the field via UI. Names like "Category_x0020_Published" are the supposed internal names of the fields.

If you plan to have more than 5000 items in your list, consider selecting top 100 order by news date desc, and then pick 3 most recent of the categories using Linq-to-Objects, like this (not tested):

   var items = list.GetItems(new SPQuery() {
       Query="<OrderBy><FieldRef Name=\"News_x0020_Published\" Ascending=\"FALSE\"></OrderBy>",
   MyRepeater.DataSource = items
       .GroupBy(li => (string)li["News_x0020_Category"])
       .Select(g => g.First())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.