Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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 = 
@"<Where>
  <Eq>
    <FieldRef Name=""News_x0020_Category"" />
    <Value Type=""Text"">{0}</Value>
  </Eq>
</Where>
<OrderBy>
  <FieldRef Name=""News_x0020_Published"" Ascending=""FALSE"" />
</OrderBy>";

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

// here you might also want to categories

MyRepeater.DataSource = new [] { 
  category1MostRecentItem,
  category2MostRecentItem,
  category3MostRecentItem
};
MyRepeater.DataBind();

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>",
       RowLimit=100 
   });
   MyRepeater.DataSource = items
       .Cast<SPListItem>()
       .GroupBy(li => (string)li["News_x0020_Category"])
       .Select(g => g.First())
       .ToArray();
share|improve this answer
    
Thanks for the solution. –  Naveen Jan 1 at 10:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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