Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am using a web service and am trying to return the highest ID number from the Posts list with viewname {B9212691-1AF1-41AF-9DA7-6073ADDB091B}. So far I have the following.

<script type="text/javascript">
    $(document).ready(function() {
        var soapEnv =
            "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
                <soapenv:Body> \
                     <GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
                        <listName>Posts</listName> \
                        <viewName>{B9212691-1AF1-41AF-9DA7-6073ADDB091B}</viewName> \
                    <View> \
                        <Query> \
                                 <OrderBy> \
                                <FieldRef Name='ID' Ascending='False' /> \
                             </OrderBy> \
                        </Query> \
                    <RowLimit>1</RowLimit> \
                    </View> \
                    </GetListItems> \
                </soapenv:Body> \
            </soapenv:Envelope>";

        $.ajax({
            url: "_vti_bin/lists.asmx",
            type: "POST",
            dataType: "xml",
            data: soapEnv,
            complete: processResult,
            contentType: "text/xml; charset=\"utf-8\""
        });
    });


    function processResult(xData, status) {
        $(xData.responseXML).find("z\\:row").each(function() {
            var liHtml = "<li>" + $(this).attr("ows_ID") + "</li>";
            $("#tasksUL").append(liHtml);
        });
    }


</script>

<ul id="tasksUL"/> 

As is, it returns all the the IDs of the view.

share|improve this question
1  
Does this code work? Are you getting any error? Please, clarify your particular problem. –  Andrey Markeev Aug 28 '11 at 22:23
    
Sorry, left that out. Edited it. –  Rick Aug 29 '11 at 13:01
add comment

1 Answer 1

Rick: I answered this for you over on Stack Overflow the other day: http://stackoverflow.com/questions/7210294/sharepoint-trying-to-get-the-latest-id/7210754#7210754

It looks like you've got the OrderBy and RowLimit correct to get what you want. My guess is that you're getting the last ID, but not in the view you want. You probably need to add any additional filtering you are doing in the view into the Query here as well.

p.s. To be compatible with multiple browsers, you should use this selector:

$(xData.responseXML).find("[nodeName='z:row']").each(function()

See my post: http://sympmarc.com/2009/11/08/sharepoints-web-services-jquery-and-the-zrow-namespace-in-safari-and-chrome/

EDIT: One other thought: Because you are specifying the RowLimit and Query, you are overriding the normal settings of the view. You could stick with the view specification only, as you had originally, and upon receiving the items in the view, in your .each() loop through and determine the highest ID in your script. Basically, you'd be building your own little MAX function.

share|improve this answer
    
I left the view in because the latest ID could be hidden and the view has the correct "latest" ID. Thanks for the syntax revision. –  Rick Aug 29 '11 at 16:32
    
Instead of eachfunction() can I just run it once then? Either way the current CAML reteives all view records. –  Rick Aug 29 '11 at 16:33
    
If you remove the query and the rowlimit, you ought to get only the items in the view. –  Marc D Anderson Aug 29 '11 at 17:05
    
But I want just the max ID from my view. –  Rick Sep 5 '11 at 23:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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