Вопрос

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');     }}
Это было полезно?

Решение

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

Другие советы

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.

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с sharepoint.stackexchange
scroll top