4

I need to get data from two customs lists that I created. However only the first list item is queried allowing me to use the data in the Home Page list item only, but I need both.

var listItems;
var listItemPics;
function GetListItems(SPHostUrl){
    var SPContext = new SP.ClientContext.get_current(SPHostUrl);
    var web = SPContext.get_web();
    var List = web.get_lists().getByTitle("Home Page");
    var picsList = web.get_lists().getByTitle("Pictures");

    var query = new SP.CamlQuery();
    query.set_viewXml('<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + 
        '<Value Type=\'Number\'>0</Value></Geq></Where></Query><RowLimit>30</RowLimit></View>');

    listItems = List.getItems(query);
    listItemPics = picsList.getItems(query); 
    SPContext.load(listItems);
    SPContext.load(listItemPics);

    SPContext.executeQueryAsync(GetListItems_Successor, GetListItems_Fail);
}

GetItemsFunction

function GetListItems_Successor(sender, args){

    var listItemInfo = '';
    var listItemEnumerator = listItem.getEnumerator();
    var picItemEnumerator = listItemPics.getEnumerator();

    while (picItemEnumerator.moveNext()){

        var picsItem = picItemEnumerator.get_current();
        var title = picsItem.get_item('Title'); 
        var url = picsItem.get_item('URL');     } 

    while (listItemEnumerator.moveNext()){

            var oListItem = listItemEnumerator.get_current();
            var title = oListItem.get_item('Title');    
            var url = oListItem.get_item('URL');     }}
8
  • Some clarification, all the items in the "Home Page" list are queried, but no items for the "Pictures" list are queried.
    – John Peter
    Commented Oct 13, 2015 at 20:21
  • Multiple examples on Google for this. One such example: zimmergren.net/technical/…. This is for SSOM but should be valid for CSOM.
    – Akhoy
    Commented Oct 13, 2015 at 20:28
  • I don't want to join the queries together (like joining sql tables?). Unless it is the only way that I can pull data from both lists using caml query.
    – John Peter
    Commented Oct 13, 2015 at 20:34
  • Show the code of GetListItems_Successor Commented Oct 14, 2015 at 8:47
  • You're not using the picItemEnumerator within the while statement.
    – iOnline247
    Commented Oct 14, 2015 at 14:05

3 Answers 3

1

The reason why it wasn't working was the order in which the items lists were loaded. Correct code is:

listItems = List.getItems(query);
SPContext.load(listItems);

listItemPics = picsList.getItems(query);     
SPContext.load(listItemPics);

Thanks

0

I can load two lists by one CAML and by one SPContext.executeQueryAsync call. It matters how your accessing your above two lists inside GetListItems_Successor.

function GetListItems_Successor() {
    console.log(listItems.get_count());
    console.log(listItemPics.get_count());
}

This gave me the total items of the lists. I hope items of the lists can be also accessible inside GetListItems_Successor. You can load too many things in SPContext. just finally execute SPContext.executeQueryAsync once. When you are executing same CAML query on multiple lists, then make sure all lists contain same column. I was your query. It should execute on both list.

4
  • I believe that the problem is before GetListItems_Successor is called.
    – John Peter
    Commented Oct 14, 2015 at 13:46
  • I run the same code as your's and it gave me results Commented Oct 14, 2015 at 13:47
  • I believe that the problem is before GetListItems function is called. In the debugger here is what is contained in the objects: 'List = SP.List {$0_0: SP.ClientContext, $2_0: SP.ClientObjectData}, camlQuery = SP.CamlQuery {$S_1: true, $R_1: "<View><Query>....."}' and 'picsList = SP.List {$0_0: SP.ClientContext, $2_0: SP.ClientObjectData}' and for SPContext.load(listItems) is 'SPContext = SP.ClientContext {$1h_0: Object, $u_0: "/sites/Technician", $1H_0: true, $2D_0: false, $1r_0: Object…}' but SP.Context.load(listItemPics) is empty, nothing shows up in the debugger.
    – John Peter
    Commented Oct 14, 2015 at 13:53
  • what did you write inside GetListItems_Successor? show it in your question Commented Oct 14, 2015 at 14:01
0

It's likely that there is not an item with an id of 0 in both lists like your CAML query is filtering for.

If you know the IDs of the items you want you should just use the List.getItemById(1) method to get individual items and avoid creating query XML manually.

If you want to make the custom query XML, I would use SP.CamlQuery.createAllItemsQuery() to start debugging the loading of the items.

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.