开发者

Accessing last record in a sharepoint list via CAML query

开发者 https://www.devze.com 2022-12-10 16:21 出处:网络
i ve some problem in accessing last record from s开发者_开发技巧harepoint list through CAML query,can any one help me in this regrad;i have a sample list called \'MainHeads\' which contains the inform

i ve some problem in accessing last record from s开发者_开发技巧harepoint 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消