Sharepoint上有多个listItemEnumerator问题

时间:2018-08-08 20:14:25

标签: javascript sharepoint sharepoint-2013 caml

我试图在页面上显示两个不同的列表,似乎2 listItemEnumerator发生冲突。如果我除去第二个枚举器(listItemEnumerator1.moveNext)以外的所有内容,它似乎可以正常工作。我尝试了各种组合,但无法同时显示两个列表。如果我要工作,则显示在代码“顶部”的那个。

我的代码:

$(function () {     
    ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");     
});

function retrieveListItems() {     
    var clientContext = new SP.ClientContext();     
    var oList = clientContext.get_web().get_lists().getByTitle('News Archives');
    var oList1 = clientContext.get_web().get_lists().getByTitle('Links');     
    var camlQuery = new SP.CamlQuery();
    var camlQuery1 = new SP.CamlQuery();
    camlQuery.set_viewXml(
        '<View><Query><Where><Leq><FieldRef Name="Publication_x0020_Date"  /><Value Type="DateTime"><Today /></Value></Lq></Where><OrderBy><FieldRef Name="Publication_x0020_Date" Ascending="False"/></OrderBy></Query><RowLimit>20</RowLimit></View>'         
    );
    this.collListItem = oList.getItems(camlQuery);
    camlQuery1.set_viewXml(
        '<View><Query><OrderBy><FieldRef Name="SortOrder" Ascending="True"/></OrderBy><RowLimit>1</RowLimit></Query></View>'         
    );     
    this.collListItem1 = oList.getItems(camlQuery1);     
    clientContext.load(collListItem);
    clientContext.load(collListItem1);     
    clientContext.executeQueryAsync(     
        Function.createDelegate(this, this.onQuerySucceeded),     
        Function.createDelegate(this, this.onQueryFailed)     
    );     
}

function onQuerySucceeded(sender, args) {     
    var listItemInfo = '';
    var listItemInfo1 = '';     
    var listItemEnumerator = collListItem.getEnumerator();
    var listItemEnumerator1 = collListItem1.getEnumerator();        
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        var publicationDate = oListItem.get_item('Publication_x0020_Date');
        var formatDate = publicationDate.format('d MMMM yyyy');                     
        listItemInfo += 
        '<div class="newsitem">' +
        '<h1 class="news-title"><a href="' + oListItem.get_item('Title0').get_url() + '">' + oListItem.get_item('Title0').get_description() + '</a></h1>' +            
        '<p>' + oListItem.get_item('Title') +  ', ' + formatDate + '</p>' +
         oListItem.get_item('Summary_x0020__x002b__x0020_Medi') +                      
        '</div>';            
    }
    while (listItemEnumerator1.moveNext()) {            
    var oListItem1 = listItemEnumerator1.get_current();
    listItemInfo1 += 
            '<a class= "button  '+ oListItem1.get_item('ycob') +'" href="' + oListItem1.get_item('Link').get_url() + '">' + oListItem1.get_item('Link').get_description() + '</a>';
    }
    $("#newsfeed").html(listItemInfo);
    $("#newsfeed").html(listItemInfo);
}

我的逻辑有何缺陷?

1 个答案:

答案 0 :(得分:1)

我如下修改您的代码以供参考,将代码添加到脚本编辑器Web部件中以检查其是否有效。

<script src="//code.jquery.com/jquery-3.3.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {     
    ExecuteOrDelayUntilScriptLoaded(retrieveListItems, "sp.js");     
});

function retrieveListItems() {     
    var clientContext = new SP.ClientContext();     
    var oList = clientContext.get_web().get_lists().getByTitle('News Archives');
    var oList1 = clientContext.get_web().get_lists().getByTitle('Links');     
    var camlQuery = new SP.CamlQuery();
    var camlQuery1 = new SP.CamlQuery();
    camlQuery.set_viewXml(
        '<View><Query><Where><Leq><FieldRef Name="Publication_x0020_Date"  /><Value Type="DateTime"><Today /></Value></Lq></Where><OrderBy><FieldRef Name="Publication_x0020_Date" Ascending="False"/></OrderBy></Query><RowLimit>20</RowLimit></View>'         
    );    
    camlQuery1.set_viewXml(
        '<View><Query><OrderBy><FieldRef Name="SortOrder" Ascending="True"/></OrderBy><RowLimit>1</RowLimit></Query></View>'         
    );     
    this.collListItem = oList.getItems(camlQuery);
    clientContext.load(collListItem);
    this.collListItem1 = oList1.getItems(camlQuery1);   
    clientContext.load(collListItem1);     
    clientContext.executeQueryAsync(     
        Function.createDelegate(this, this.onQuerySucceeded),     
        Function.createDelegate(this, this.onQueryFailed)     
    );     
}

function onQuerySucceeded(sender, args) {     
    var listItemInfo = '';
    var listItemInfo1 = '';     
    var listItemEnumerator = collListItem.getEnumerator();
    var listItemEnumerator1 = collListItem1.getEnumerator();        
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        var publicationDate = oListItem.get_item('Publication_x0020_Date');
        var formatDate = publicationDate.format('d MMMM yyyy');                     
        listItemInfo += 
        '<div class="newsitem">' +
        '<h1 class="news-title"><a href="' + oListItem.get_item('Title0').get_url() + '">' + oListItem.get_item('Title0').get_description() + '</a></h1>' +            
        '<p>' + oListItem.get_item('Title') +  ', ' + formatDate + '</p>' +
         oListItem.get_item('Summary_x0020__x002b__x0020_Medi') +                      
        '</div>';            
    }
    while (listItemEnumerator1.moveNext()) {            
        var oListItem1 = listItemEnumerator1.get_current();
        listItemInfo1 += 
                '<a class= "button  '+ oListItem1.get_item('ycob') +'" href="' + oListItem1.get_item('Link').get_url() + '">' + oListItem1.get_item('Link').get_description() + '</a>';
    }
    $("#newsfeed").html("");
    $("#newsfeed").append(listItemInfo);
    $("#newsfeed").append(listItemInfo1);
}
function onQueryFailed(sender, args) {
    console.log('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
<div id="newsfeed"/>