1

I created a Custom List in developer Site. In that List I created a column called Totalsumma and added some numeric items like "324", "567" etc. How can I fetch the highest value from that column.

    function totalSumma() {
    var query = new SP.CamlQuery();
    var textCaml = "<View><Query><OrderBy><FieldRef Name='fmld' Ascending='False' /></OrderBy></Query><RowLimit>1</RowLimit></View>";
    query.set_viewXml(textCaml);
    var items = listitemCollection.getItems(query);
    context.load(items)

    context.executeQueryAsync(onsuccess, onfail);
    context.load(listitemCollection);

    function onsuccess() {

        var innerHtml = '';
        var enumerator = listitemCollection.getEnumerator();

        while (enumerator.moveNext()) {
            var currentListItem = enumerator.get_current();
            innerHtml += "<br/>" + currentListItem.get_item('fmld');
        }

        $("#Visa4").html(innerHtml);
    }

    function onfail() {
        alert("wrong");
    }
}

1 Answer 1

1

You can use below query

<View>
    <Query>        
        <OrderBy>
            <FieldRef Name='fmld' Ascending='FALSE' />
        </OrderBy>
    </Query>
    <RowLimit>1</RowLimit>
</View>

Then use

query.set_viewXml(query)
items = list.getItems(query)
context.load(items)

Updated based on answer edit

function totalSumma() {
    var query = new SP.CamlQuery();
    var textCaml = "<View><Query><OrderBy><FieldRef Name='fmld' Ascending='FALSE' /></OrderBy></Query><RowLimit>1</RowLimit></View>";
    query.set_viewXml(textCaml);
    var items = list.getItems(query);
    context.load(items);
    context.executeQueryAsync(onsuccess, onfail);
    function onsuccess() {
        var innerHtml = '';
        var enumerator = items.getEnumerator();
        while (enumerator.moveNext()) {
            var currentListItem = enumerator.get_current();
            innerHtml += "<br/>" + currentListItem.get_item('fmld');
        }
        $("#Visa4").html(innerHtml);
    }
    function onfail() {
        alert("wrong");
    }
}
10
  • Okey I used it but it displays: "1" in the div. Any wrong with my function?
    – Sunreuz
    Commented May 31, 2015 at 12:01
  • Doesnt work : (
    – Sunreuz
    Commented May 31, 2015 at 12:44
  • Ok I have updated now.
    – Sunreuz
    Commented May 31, 2015 at 12:47
  • var listitemCollection = parentWeb.get_lists().getByTitle("SpTenta1Uppg2").getItems("");
    – Sunreuz
    Commented May 31, 2015 at 12:53
  • That code is global.
    – Sunreuz
    Commented May 31, 2015 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.