1

I am trying to retrieve data from two different list in SharePoint. I have created one page layout and ,I already made the code , I checked each function is working fine , but when I use both of them only last one will work, can you please help regarding this :

 <div id="NewsContainer" style="width: 100%"></div>
<div id="GeneralMessagesContainer" style="width:100%"></div>
            <script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
            <script type="text/javascript" src="_layouts/15/sp.runtime.js"></script>
            <script type="text/javascript" src="_layouts/15/sp.js"></script>
            <script type="text/javascript" src="https://code.jquery.com/jquery-2.2.4.min.js"></script>


            <script type="text/javascript">

                var jq = $.noConflict();
                jq(function () {

                    GetGMMessage();
                    GetUserInfo();
                });

                //$(function () {
                //    GetUserInfo();
                //});
                function GetUserInfo() {
                    var clientContext = new SP.ClientContext.get_current();
                    var oList = clientContext.get_web().get_lists().getByTitle('News');

                    //Start
                    var camlQuery = new SP.CamlQuery();
                    camlQuery.set_viewXml('<View><Query><OrderBy><FieldRef Name=\'ID\' Ascending=\'FALSE\' /></OrderBy></Query><RowLimit>10</RowLimit></View>');
                    //END

                    this.collListItem = oList.getItems(camlQuery);

                    clientContext.load(collListItem);
                    clientContext.executeQueryAsync(
                    Function.createDelegate(this, this.onQuerySucceeded),
                    Function.createDelegate(this, this.onQueryFailed)
                    );
                }
                function GetGMMessage() {
                    var clientContext = new SP.ClientContext.get_current();
                    var oListGM = clientContext.get_web().get_lists().getByTitle('GeneralMessages');

                    //Start
                    var camlQuery = new SP.CamlQuery();
                    camlQuery.set_viewXml('<View><Query><OrderBy><FieldRef Name=\'FormatMessageDate\' Ascending=\'FALSE\' /></OrderBy></Query><RowLimit>1</RowLimit></View>');
                    //END

                    this.collListItem = oListGM.getItems(camlQuery);

                    clientContext.load(collListItem);
                    clientContext.executeQueryAsync(
                    Function.createDelegate(this, this.onQuerySucceededGM),
                    Function.createDelegate(this, this.onQueryFailedGM)
                    );
                }
                function onQuerySucceededGM(sender, args) {

                    var listItemEnum = collListItem.getEnumerator();

                    //var NewsItemsList = '<thead>' +
                    //'<tr>' +
                    //'</tr>' +
                    //'</thead>';
                    var MessageItemsList = '<div class="News-Item">';
                    while (listItemEnum.moveNext()) {
                        var oListItemGM = listItemEnum.get_current();
                        MessageItemsList +=
                            '<a class="News-Title" href="/sites/demo/Lists/TestNews/DispForm.aspx?ID='
                            + oListItemGM.get_item('ID') + '">'
                            + oListItemGM.get_item('Title')
                            + '<div class="clear"></div>'
                            + '</a>'
                        ;
                    }
                    MessageItemsList += '</div>';
                    $('#GeneralMessagesContainer').html(MessageItemsList);
                }
                function onQueryFailedGM(sender, args) {
                    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
                }
                function onQuerySucceeded(sender, args) {

                    var listItemEnum = collListItem.getEnumerator();


                    var NewsItemsList = '<div class="News-Item">';
                    while (listItemEnum.moveNext()) {
                        var oListItem = listItemEnum.get_current();
                        var Desc = oListItem.get_item('Summary');
                        Desc = Desc.substring(0,300);
                        NewsItemsList += 
                            '<a class="News-Title" href="/sites/demo/Lists/TestNews/DispForm.aspx?ID='
                            + oListItem.get_item('ID') + '">'
                            + oListItem.get_item('PublishingRollupImage')
                            + oListItem.get_item('Title')
                            + '<p style="font-size:10px;font-weight:normal"> Article Date : '
                            + oListItem.get_item('Article_x0020_Date').toISOString().slice(0, 10) + ' , By :'
                            + oListItem.get_item('Posting_x0020_Department') + '</p>'
                            + '<p style="font-size:13px;font-weight:normal">' + Desc + "</p>"
                            + '<div class="clear"></div>'
                            + '</a>'

                        ;
                    }
                    NewsItemsList += '</div>';
                    $('#NewsContainer').html(NewsItemsList);
                }
                function onQueryFailed(sender, args) {
                    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
                }
            </script>

thanks for the answers guys , here is the full updated answer :

<div id="GeneralMessagesContainer" style="width:100%"></div>
            <div id="NewsContainer" style="width: 100%"></div>

            <script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
            <script type="text/javascript" src="_layouts/15/sp.runtime.js"></script>
            <script type="text/javascript" src="_layouts/15/sp.js"></script>
            <script type="text/javascript" src="https://code.jquery.com/jquery-2.2.4.min.js"></script>

            <script type="text/javascript">

            var jq = $.noConflict();
            jq(function () {
                GetNewsList();
                GetGMMessage();                   
            });


            function GetNewsList() {
                var clientContextNewsList = new SP.ClientContext.get_current();
                var oList = clientContextNewsList.get_web().get_lists().getByTitle('News');

                //Start
                var camlQuery = new SP.CamlQuery();
                camlQuery.set_viewXml('<View><Query><OrderBy><FieldRef Name=\'ID\' Ascending=\'FALSE\' /></OrderBy></Query><RowLimit>10</RowLimit></View>');
                //END

                this.collListItem = oList.getItems(camlQuery);

                clientContextNewsList.load(collListItem);
                clientContextNewsList.executeQueryAsync(
                Function.createDelegate(this, this.onQuerySucceeded),
                Function.createDelegate(this, this.onQueryFailed)
                );
            }
            function onQuerySucceeded(sender, args) {

                var listItemEnum = collListItem.getEnumerator();


                var NewsItemsList = '<div class="News-Item">';
                while (listItemEnum.moveNext()) {
                    var oListItem = listItemEnum.get_current();
                    var Desc = oListItem.get_item('Summary');
                    Desc = Desc.substring(0,300);
                    NewsItemsList +=
                        '<a class="News-Title" href="/sites/demo/Lists/TestNews/DispForm.aspx?ID='
                        + oListItem.get_item('ID') + '">'
                        + oListItem.get_item('PublishingRollupImage')
                        + oListItem.get_item('Title')
                        + '<p style="font-size:10px;font-weight:normal"> Article Date : '
                        + oListItem.get_item('Article_x0020_Date').toISOString().slice(0, 10) + ' , By :'
                        + oListItem.get_item('Posting_x0020_Department') + '</p>'
                        + '<p style="font-size:13px;font-weight:normal">' + Desc + "</p>"
                        + '<div class="clear"></div>'
                        + '</a>'

                    ;
                }
                NewsItemsList += '</div>';
                $('#NewsContainer').html(NewsItemsList);
            }
            function onQueryFailed(sender, args) {
                alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
            }

            function GetGMMessage() {
                var clientContextGM = new SP.ClientContext.get_current();
                var oListGM = clientContextGM.get_web().get_lists().getByTitle('GeneralMessages');

                //Start
                var camlQuery = new SP.CamlQuery();
                camlQuery.set_viewXml('<View><Query><OrderBy><FieldRef Name=\'FormatMessageDate\' Ascending=\'FALSE\' /></OrderBy></Query><RowLimit>1</RowLimit></View>');
                //END

                this.collListItem1 = oListGM.getItems(camlQuery);

                clientContextGM.load(collListItem1);
                clientContextGM.executeQueryAsync(
                Function.createDelegate(this, this.onQuerySucceededGM),
                Function.createDelegate(this, this.onQueryFailedGM)
                );
            }
            function onQuerySucceededGM(sender, args) {

                var listItemEnum = collListItem1.getEnumerator();

                var MessageItemsList = '<div class="News-Item">';
                while (listItemEnum.moveNext()) {
                    var oListItemGM = listItemEnum.get_current();
                    MessageItemsList +=
                        '<a class="News-Title" href="/sites/demo/Lists/TestNews/DispForm.aspx?ID='
                        + oListItemGM.get_item('ID') + '">'
                        + oListItemGM.get_item('Title')
                        + '<div class="clear"></div>'
                        + '</a>'
                    ;
                }
                MessageItemsList += '</div>';
                $('#GeneralMessagesContainer').html(MessageItemsList);
            }
            function onQueryFailedGM(sender, args) {
                alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
            }
            </script>
3

give different name to your collection object(collListItem) name in both function.

enter image description here

enter image description here

in javascript or jQuery first run both function(GetUserInfo, GetGMMessage) than run both success function so collection object(collListItem) value overwrite by another function value. so. at last display only last success function value

1

Try below code:

<div id="NewsContainer" style="width: 100%"></div>
<div id="GeneralMessagesContainer" style="width:100%"></div>
<script type="text/javascript" src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"></script>
<script type="text/javascript" src="_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="_layouts/15/sp.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-2.2.4.min.js"></script>


<script type="text/javascript">

    var jq = $.noConflict();
    jq(function () {

        GetGMMessage();
        GetUserInfo();
    });

    //$(function () {
    //    GetUserInfo();
    //});
    function GetUserInfo() {
        var clientContextUI = new SP.ClientContext.get_current();
        var oListUI = clientContextUI.get_web().get_lists().getByTitle('News');

        //Start
        var camlQueryUI = new SP.CamlQuery();
        camlQueryUI.set_viewXml('<View><Query><OrderBy><FieldRef Name=\'ID\' Ascending=\'FALSE\' /></OrderBy></Query><RowLimit>10</RowLimit></View>');
        //END

        this.collListItemUI = oListUI.getItems(camlQueryUI);

        clientContext.load(collListItemUI);
        clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded),
        Function.createDelegate(this, this.onQueryFailed)
        );
    }
    function GetGMMessage() {
        var clientContextGM = new SP.ClientContext.get_current();
        var oListGM = clientContextGM.get_web().get_lists().getByTitle('GeneralMessages');

        //Start
        var camlQueryGM = new SP.CamlQuery();
        camlQueryGM.set_viewXml('<View><Query><OrderBy><FieldRef Name=\'FormatMessageDate\' Ascending=\'FALSE\' /></OrderBy></Query><RowLimit>1</RowLimit></View>');
        //END

        this.collListItemGM = oListGM.getItems(camlQueryGM);

        clientContext.load(collListItemGM);
        clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceededGM),
        Function.createDelegate(this, this.onQueryFailedGM)
        );
    }
    function onQuerySucceededGM(sender, args) {

        var listItemEnumGM = collListItemGM.getEnumerator();

        //var NewsItemsList = '<thead>' +
        //'<tr>' +
        //'</tr>' +
        //'</thead>';
        var MessageItemsList = '<div class="News-Item">';
        while (listItemEnumGM.moveNext()) {
            var oListItemGM = listItemEnumGM.get_current();
            MessageItemsList +=
                '<a class="News-Title" href="/sites/demo/Lists/TestNews/DispForm.aspx?ID='
                + oListItemGM.get_item('ID') + '">'
                + oListItemGM.get_item('Title')
                + '<div class="clear"></div>'
                + '</a>'
            ;
        }
        MessageItemsList += '</div>';
        $('#GeneralMessagesContainer').html(MessageItemsList);
    }
    function onQueryFailedGM(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
    function onQuerySucceeded(sender, args) {

        var listItemEnumUI = collListItemUI.getEnumerator();

        var NewsItemsList = '<div class="News-Item">';
        while (listItemEnumUI.moveNext()) {
            var oListItem = listItemEnumUI.get_current();
            var Desc = oListItem.get_item('Summary');
            Desc = Desc.substring(0,300);
            NewsItemsList += 
                '<a class="News-Title" href="/sites/demo/Lists/TestNews/DispForm.aspx?ID='
                + oListItem.get_item('ID') + '">'
                + oListItem.get_item('PublishingRollupImage')
                + oListItem.get_item('Title')
                + '<p style="font-size:10px;font-weight:normal"> Article Date : '
                + oListItem.get_item('Article_x0020_Date').toISOString().slice(0, 10) + ' , By :'
                + oListItem.get_item('Posting_x0020_Department') + '</p>'
                + '<p style="font-size:13px;font-weight:normal">' + Desc + "</p>"
                + '<div class="clear"></div>'
                + '</a>'

            ;
        }
        NewsItemsList += '</div>';
        $('#NewsContainer').html(NewsItemsList);
    }
    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
    }
</script>

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.