views:

507

answers:

3

hi,i ve some problem in accessing last record from sharepoint list through CAML query,can any one help me in this regrad;i have a sample list called 'MainHeads' which contains the information like HeadID,Category,and headName....

Mehboob Pakistan

+1  A: 
<View>
<RowLimit>1</RowLimit>
<Query>
   <OrderBy>
      <FieldRef Name='Created' Ascending='False' />
   </OrderBy>
</Query>
</View>
F.Aquino
An alternative could be the ID Column, which (AFAIK) is also available on every item.
Michael Stum
A: 

Building on this answer I gave to 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 executing the CAML in code. IF not, see F. Aquino's answer.

MagicAndi
A: 
<View> 
<RowLimit>1</RowLimit> 
<Query> 
   <OrderBy> 
      <FieldRef Name='ID' Ascending='False' /> 
   </OrderBy> 
</Query> 
</View>
Hojo