2

I get only first item from list using below code.I want all items from list using below code.I am passing value in Caml query through while loop one by one.How to get all values from list?Thank you.

    var totalID = [];
var deferred;

function GetTitleIdea() {

    var dfd = $.Deferred();
    var clientContext = new SP.ClientContext.get_current();
    var listIdeas = clientContext.get_web().get_lists().getByTitle('Ideas');
    var camlquery = new SP.CamlQuery();
    ListItems = listIdeas.getItems(camlquery);
    clientContext.load(ListItems);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceededinsertValue), Function.createDelegate(this, this.onQueryFailedinsertValue));
    //clientContext.executeQueryAsync(onQuerySucceededinsertValue, onQueryFailedinsertValue);
    return dfd.promise();
}

function onQuerySucceededinsertValue() {
    debugger;
    var enumeratorList = ListItems.getEnumerator();
    while (enumeratorList.moveNext()) {
        debugger;

        var Item = enumeratorList.get_current();
        var ID = Item.get_item('ID');
        deferred = $.Deferred();
        var contextIdeas = new SP.ClientContext();
        var list = contextIdeas.get_web().get_lists().getByTitle('Ideas');
        var caml = new SP.CamlQuery();
        caml.set_viewXml("<View><Query><Where><Eq><FieldRef Name='ID'/><Value Type='Integer'>" + ID + "</Value></Eq></Where></Query></View>");
        returnedItemsIdeas = list.getItems(caml);
        contextIdeas.load(returnedItemsIdeas);
        contextIdeas.executeQueryAsync(Function.createDelegate(this, this.sucess), Function.createDelegate(this, this.failed));
        return deferred.promise();
    }

    dfd.resolve();
}

function onQueryFailedinsertValue(sender, args) {
    dfd.reject(sender, args);
}

function sucess(sender, args) {
    debugger;
    var enumeratorIdeas = returnedItemsIdeas.getEnumerator();
    while (enumeratorIdeas.moveNext()) {
        var listItemIdeas = enumeratorIdeas.get_current();
        Title = listItemIdeas.get_item('Title');
        alert(Title);
    }
    deferred.resolve();
}

function failed(sender, args) {
    deferred.reject(sender, args);
}
0
function onQuerySucceededinsertValue() {
    debugger;
    var enumeratorList = ListItems.getEnumerator();
    while (enumeratorList.moveNext()) {
        debugger;

        var Item = enumeratorList.get_current();
        var ID = Item.get_item('ID');
        deferred = $.Deferred();
        var contextIdeas = new SP.ClientContext();
        var list = contextIdeas.get_web().get_lists().getByTitle('Ideas');
           var caml = new SP.CamlQuery();
           var items = list.getItems(caml);
           contextIdeas.load(items);
        returnedItemsIdeas = list.getItems(caml);
        contextIdeas.load(returnedItemsIdeas);
        contextIdeas.executeQueryAsync(onQuerySucceeded, onQueryFailed)

             function onQuerySucceeded() {
              success(items, ID);
             }
    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() +            
    }
        };
        return deferred.promise();
    }
    dfd.resolve();
}
function sucess(items, ID) {
    debugger;
    var enumeratorIdeas = items.getEnumerator();
    while (enumeratorIdeas.moveNext()) {
        var listItemIdeas = enumeratorIdeas.get_current();
        Title = listItemIdeas.get_item('Title');
        alert(Title);
    }
    deferred.resolve();
}

Hope this will be usefull

  • Thank You for Your response. I want the filter a list through a CAML query. I want only those titles whose ID we pass in CAML Query. – Varsha Aug 17 '17 at 4:18
  • if, the camlquery isn't working then you can use this approach where you will iterate through all records from list and check the condition to get data as per the Id which you are passing to Caml query – Madhav Aug 17 '17 at 9:45
  • After trial and error I got solution following is my working code : – Varsha Dec 8 '17 at 9:07
0

Replace following line of code

 var camlquery = new SP.CamlQuery();

with this.

 var camlquery = SP.CamlQuery.createAllItemsQuery();
  • Thank You for your quick response. I changed my code according to you but still i get same response.I got only first item from list. – Varsha Aug 16 '17 at 8:55

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.