Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing last record in a sharepoint list via CAML query

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

like image 457
Mehboob Ali Avatar asked Nov 04 '09 04:11

Mehboob Ali


3 Answers

<View>
<RowLimit>1</RowLimit>
<Query>
   <OrderBy>
      <FieldRef Name='Created' Ascending='False' />
   </OrderBy>
</Query>
</View>
like image 165
Francisco Aquino Avatar answered Nov 07 '22 13:11

Francisco Aquino


<View> 
<RowLimit>1</RowLimit> 
<Query> 
   <OrderBy> 
      <FieldRef Name='ID' Ascending='False' /> 
   </OrderBy> 
</Query> 
</View>
like image 38
Hojo Avatar answered Nov 07 '22 14:11

Hojo


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.

like image 2
Tangiest Avatar answered Nov 07 '22 14:11

Tangiest