[英]MAX query using CAML
I want to select a sharepoint list item which has the Maximum value for a particular column. 我想选择一个具有特定列的最大值的共享点列表项。 How can I do this using CAML queries?
如何使用CAML查询执行此操作?
The following CAML query would return the maximum value for a given column: 以下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;
<Query>
<OrderBy>
<FieldRef Name="particularcolumn" Ascending="FALSE" />
</OrderBy>
</Query>
这可以通过此字段以降序方式进行排序并返回集合的第一个元素。
Using the client object model, this is how I'm retrieving the max ID from a list. 使用客户端对象模型,这就是我从列表中检索最大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();
}
<script type="text/javascript" charset="utf8" src="/jquery-3.2.1.min.js"></script>
<script type="text/javascript" charset="utf8" src="/jquery.SPServices-2014.02.min.js"></script>
<script type="text/javascript">
function getLastItemId() {
var userId = _spPageContextInfo.userId;
var caml = "<View><Query><Where>"
+ "<Eq><FieldRef Name='Author' LookupId='TRUE' /><Value Type='Integer'>"
+ userId + "</Value></Eq></Where>"
+ "<OrderBy><FieldRef Name='Created' Ascending='False' /></OrderBy>"
+ "</Query><RowLimit>1</RowLimit></View>";
var ctx = SP.ClientContext.get_current()
var web = ctx.get_web()
//ENTER YOUR LIST NAME BELOW
var list = web.get_lists().getByTitle("YOUR LIST NAME")
var query = new SP.CamlQuery();
query.set_viewXml(caml);
var items = list.getItems(query);
ctx.load(items)
ctx.executeQueryAsync(function() {
// success actions
var count = items.get_count();
//should only be 1
if (count > 1) {
throw "Something is wrong. Should only be one latest list item / doc";
}
var enumerator = items.getEnumerator();
enumerator.moveNext();
var item = enumerator.get_current();
var id = item.get_id();
// do something with your result!!!!
alert(id);
}, function() {
//failure handling comes here
alert("failed");
});
}
getLastItemId();
</script>
<button id="button1" type="button" onclick="getLastItemId();">click to get last ID</button>
I managed to get the following working I used a script web part and added the following to the web part. 我设法得到以下工作我使用了脚本Web部件并将以下内容添加到Web部件中。 When you click the button an alert will pop up with the highest item ID number.
单击该按钮时,将弹出一个警报,其中包含最高的项目ID号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.