0

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.

2
  • 1
    Does this code work? Are you getting any error? Please, clarify your particular problem. Commented Aug 28, 2011 at 22:23
  • Sorry, left that out. Edited it.
    – Rick
    Commented Aug 29, 2011 at 13:01

2 Answers 2

2

Rick: I answered this for you over on Stack Overflow the other day: https://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.

4
  • 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
    Commented Aug 29, 2011 at 16:32
  • Instead of eachfunction() can I just run it once then? Either way the current CAML reteives all view records.
    – Rick
    Commented Aug 29, 2011 at 16:33
  • If you remove the query and the rowlimit, you ought to get only the items in the view. Commented Aug 29, 2011 at 17:05
  • But I want just the max ID from my view.
    – Rick
    Commented Sep 5, 2011 at 23:52
0

It works fine. I did programatically in SPSevices.

$(document).ready(function() {

    var idquery=""; 
    $().SPServices({
     operation: "GetListItems", 
    async: false, 
    listName: "test", 
    CAMLQuery: idquery, 
    CAMLRowLimit: 1, 
    completefunc: function (xData, Status) 
    { 
    $(xData.responseXML).SPFilterNode("z:row").each(function() { //alert($(this).attr("ows_Title")); 
    var getvalue= parseInt($(this).attr("ows_Title"))+1;//Auto increment value $("input[title='TicketNumber']").val(getvalue);

            });
         } 
        });
  1. Get value in variable parseInt($(this).attr("ows_Title"))+1;
  2. Bind text box TICKET Number

Click here more details

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.