Demi
Demi

Reputation: 328

Retrieve the first list item of a sorted list SharePoint 2010 C#

I just started SharePoint webpart programming today. I have list called "pagesList" and I want to order its list items using the field "sortField" in descending order. Then I want to get the first item in the list. here is what I tried so far but I don't know how to get the first item in the sorted list to assign it.

if (pagesList != null)
{
    SPQuery q = new SPQuery();
    q.Query = "<OrderBy><FieldRef Name='sortField' Ascending='False' /></OrderBy>";
    SPListItemCollection listItemCollection = pagesList.GetItems(q);
    CurrentIssueLinkButton.PostBackUrl = /* the first item of the sorted listitems goes here*/;
 }  

How can I get the first list item?

Upvotes: 0

Views: 875

Answers (1)

Servy
Servy

Reputation: 203838

Set the RowLimit of the SPQuery object to one to ensure that at most one item is returned.

Upvotes: 1

Related Questions