MAX query using CAML

本秂侑毒 提交于 2019-11-30 19:57:35
<Query>
    <OrderBy>
            <FieldRef Name="particularcolumn" Ascending="FALSE" />
    </OrderBy>
</Query>

The following CAML query would return the maximum value for a given column:

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;

This can be done ordering by this field in descending way and taking the first element of the collection returned.

Using the client object model, this is how I'm retrieving the max ID from a list.

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. When you click the button an alert will pop up with the highest item ID number.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!