2

Below JSOM code is iterating through sharepoint list items. This will not be efficient if the CAML query results are in thousands. Is there any way to convert the query results directly into JSON without looping and REST queries.

while (listItemEnumerator.moveNext()) 
{
  var oListItem = listItemEnumerator.get_current();
  listItemInfo += '\nID: ' + oListItem.get_id() +
             '\nTitle: ' + oListItem.get_item('Title') +
             '\nDLastName: ' + oListItem.get_item('DLastName') +
             '\nDLastName: ' + oListItem.get_item('DLastName');
}
2

Umh, afaik that's not possible. I don't know your reasons for why you can't use REST, but you could try looking into the onExecutingWebRequest etc on the SP.ClientContext object and abort the initial request, and then do your own request with the payload JSOM have generated. Never used it like that myself, but I have a simple example of what I'm talking about that you could play around with:

(function() { 

    var ctx = SP.ClientContext.get_current();
    var list = ctx.get_web().get_lists().getByTitle("ListTitle");
    var items = list.getItems(SP.CamlQuery.createAllItemsQuery());
    ctx.load(items, "Include(ID, Title)");

    // abort the request
    ctx.set_requestTimeout(1);

    ctx.onExecutingWebRequest = function(webRequest,e) {
        console.log(webRequest)
        var waitForExecutor = setInterval(function() { 
            if(typeof  webRequest.get_webRequest() != null) {
                clearInterval(waitForExecutor);

                 $.ajax({
                    url: webRequest.get_webRequest().get_url(),
                    data: webRequest.get_webRequest().get_body(),
                    type: "POST",
                    headers: {
                        "Content-Type": webRequest.get_webRequest().get_headers()["Content-Type"], 
                        "X-RequestDigest": webRequest.get_webRequest().get_headers()["X-RequestDigest"]
                    },
                    success: function(data) {
                        console.log(data)
                        if(data.length) {
                            var collection = data.filter(function(item) { 
                                if(item._ObjectType_ === "SP.ListItemCollection") {
                                    return item._Child_Items_;
                                }
                            });
                            if(collection.length) {
                                var json = JSON.stringify(collection[0]._Child_Items_);
                                console.log(json);
                            }
                        }
                    },
                    error: function(err) {
                        console.log("ERR")
                    }
                });          
            }
        },10);

    }   

    ctx.executeQueryAsync( function() {
        console.log("Should not enter this function")
    }, function(s,a) {
        console.log("Should enter if aborted");
        console.log(a.get_message())
    });

})();
  • REST will not support complex CAML queries which involves multiple list JOINS and Projections that's why I am using JSOM – user17274 Feb 14 '18 at 6:30

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.