Access last entry in sharepoint list via CAML request

I have a problem accessing the last entry from the sharepoint list via a CAML request, can someone help me with this regrad, I have a list of examples called "MainHeads" that contains information such as HeadID, Category and headName ....

Mehbub Pakistan

+4
source share
3 answers
<View> <RowLimit>1</RowLimit> <Query> <OrderBy> <FieldRef Name='Created' Ascending='False' /> </OrderBy> </Query> </View> 
+5
source

Based on this answer , I gave a related question , I would suggest the following query

 SPListItem lastItem; try { using (SPSite objSite = new SPSite(sSiteUrl)) { using (SPWeb objWeb = objSite.OpenWeb()) { SPList objList = objWeb.Lists["MainHeads"]; SPQuery objQuery = new SPQuery(); objQuery.Query = "<OrderBy><FieldRef Name='HeadID' Ascending='False' /></OrderBy><RowLimit>1</RowLimit>"; objQuery.Folder = objList.RootFolder; // Execute the query against the list SPListItemCollection colItems = objList.GetItems(objQuery); if (colItems.Count > 0) { lastItem = colItems[0]; } } } } catch (Exception ex) { ... } return lastItem; 

This assumes that you are doing CAML in code. IF not, see F. Aquino answer .

+2
source
 <View> <RowLimit>1</RowLimit> <Query> <OrderBy> <FieldRef Name='ID' Ascending='False' /> </OrderBy> </Query> </View> 
+2
source

All Articles