2009-02-05 109 views

回答

9
<Query> 
    <OrderBy> 
      <FieldRef Name="particularcolumn" Ascending="FALSE" /> 
    </OrderBy> 
</Query> 
+6

不要忘記在查詢上設置RowLimit。 – etc 2010-06-03 15:41:02

3

可以通過此字段以降序方式進行排序,並返回收集的第一個元素。

12

下面的CAML查詢將針對給定列返回最大值:

var maxValue; 

try 
{ 
    using (SPSite objSite = new SPSite(sSiteUrl)) 
    { 
     using (SPWeb objWeb = objSite.OpenWeb()) 
     { 
      SPList objList = objWeb.Lists[sListName]; 

      SPQuery objQuery = new SPQuery(); 
      objQuery.Query = "<OrderBy><FieldRef Name='ColumnName' 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) 
      { 
       maxValue = (<Insert Appropriate Cast>) colItems[0]; 
      } 
     } 
    } 
} 
catch (Exception ex) 
{ 
    ... 
} 

return maxValue; 
+0

設置`objQuery.Folder = objList.RootFolder;`的好處是什麼? – 2014-02-11 16:44:47

0

使用客戶端對象模型,這是我如何檢索從列表中最大的ID。

using (ClientContext clientContext = new ClientContext("https://sharepointed.com")) 
    { 
     clientContext.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; 

     oWeb = clientContext.Web; 

     List myList= oWeb.Lists.GetByTitle("MyList"); 

     CamlQuery camlQuery = new CamlQuery(); 
     camlQuery.ViewXml = ("<View><Query> <OrderBy> <FieldRef Name='ID' Ascending='False' /> </OrderBy> </Query> <RowLimit>1</RowLimit> </View>"); 
     Microsoft.SharePoint.Client.ListItemCollection listItems = myList.GetItems(camlQuery); 
     clientContext.Load(listItems); 
     clientContext.ExecuteQuery(); 
    }