3

The function SPGetLastItem from SPServices returns last id for items that only the current user has created. I want a function that returns the last id for any user.

1

Well, adding the code below as a new function to the SPServices.js file, will work. The new function name is SPGetLastItemIdAnyUser :

// Function to return the ID of the last item created on a list. 
$.fn.SPServices.SPGetLastItemIdAnyUser = function(options) {

    var opt = $.extend({}, {
        webURL: "",         // URL of the target Web.  If not specified, the current Web is used.
        listName: "",           // The name or GUID of the list
        CAMLQuery: ""           // [Optional] For power users, this CAML fragment will be Anded with the default query on the relatedList
    }, options);

    var userId;
    var lastId = 0;


    // Get the list items for the user, sorted by Created, descending. If the CAMLQuery option has been specified, And it with
    // the existing Where clause
    var camlQuery = "<Query><Where>";
    if(opt.CAMLQuery.length > 0) {
        camlQuery += "<And>";
    }   

    camlQuery += "<Gt><FieldRef Name='Author' LookupId='TRUE'/><Value Type='Integer'>0</Value></Gt>";


    if(opt.CAMLQuery.length > 0) {
        camlQuery += opt.CAMLQuery + "</And>";
    }   

    camlQuery += "</Where><OrderBy><FieldRef Name='Created_x0020_Date' Ascending='FALSE'/></OrderBy></Query>";

    $().SPServices({
        operation: "GetListItems",
        async: false,
        webURL: opt.webURL,
        listName: opt.listName,
        CAMLQuery: camlQuery,
        CAMLViewFields: "<ViewFields><FieldRef Name='ID'/></ViewFields>",
        CAMLRowLimit: 1,
        CAMLQueryOptions: "<QueryOptions><ViewAttributes Scope='Recursive' /></QueryOptions>",
        completefunc: function(xData, Status) {
            $(xData.responseXML).SPFilterNode("z:row").each(function() {
                lastId = $(this).attr("ows_ID");
            });
        }
    });
    return lastId;
}; // End $.fn.SPServices.SPGetLastItemIdAnyUser

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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