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
<View>
<RowLimit>1</RowLimit>
<Query>
<OrderBy>
<FieldRef Name='Created' Ascending='False' />
</OrderBy>
</Query>
</View>
<View>
<RowLimit>1</RowLimit>
<Query>
<OrderBy>
<FieldRef Name='ID' Ascending='False' />
</OrderBy>
</Query>
</View>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With