MAX query using CAML
Asked Answered
F

5

8

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?

Franciscofranciska answered 5/2, 2009 at 14:17 Comment(0)
P
11
<Query>
    <OrderBy>
            <FieldRef Name="particularcolumn" Ascending="FALSE" />
    </OrderBy>
</Query>
Pathan answered 6/2, 2009 at 16:15 Comment(1)
Don't forget to set RowLimit on the query.Drowsy
E
12

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;
Excelsior answered 12/8, 2009 at 9:49 Comment(1)
What is the benefits of settings objQuery.Folder = objList.RootFolder; ?Schilit
P
11
<Query>
    <OrderBy>
            <FieldRef Name="particularcolumn" Ascending="FALSE" />
    </OrderBy>
</Query>
Pathan answered 6/2, 2009 at 16:15 Comment(1)
Don't forget to set RowLimit on the query.Drowsy
I
3

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

Isoclinal answered 5/2, 2009 at 22:2 Comment(0)
L
0

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();
     }
Lute answered 24/11, 2017 at 20:19 Comment(0)
H
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>

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.

Hebdomad answered 1/6, 2018 at 15:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.