8

我想选择一个共享点列表项,它具有特定列的最大值。如何使用 CAML 查询来做到这一点?

4

5 回答 5

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;
于 2009-08-12T09:49:02.533 回答
11
<Query>
    <OrderBy>
            <FieldRef Name="particularcolumn" Ascending="FALSE" />
    </OrderBy>
</Query>
于 2009-02-06T16:15:18.313 回答
3

这可以通过此字段以降序方式排序并获取返回的集合的第一个元素。

于 2009-02-05T22:02:30.390 回答
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();
     }
于 2017-11-24T20:19:13.303 回答
0
<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>

我设法让以下工作我使用了脚本 Web 部件并将以下内容添加到 Web 部件中。当您单击该按钮时,将弹出一个带有最高项目 ID 号的警报。

于 2018-06-01T15:35:21.803 回答