none
Convert JSOM code to synchronous RRS feed

  • Question

  • have the JSOM script to get data from a SharePoint list on my page of different countries in different DIV, i am calling the function in document.ready as many number of times as there are number of countries,

    but i have an alert just before i call the function and if i remove the alert(country_list[i]) code does not work.

    How can i fix this?

    <script type="text/javascript">
     $(document).ready(function(){
        var country_list = ["Belgium", "Brazil","Canada", "Germany","USA", "Other countries"];
        for(var i=0; i<country_list.length; i++)
        {
    //if i remove this alert the code does not work
            alert(country_list[i]);
            retrieveListItems_berba(country_list[i]);
        }
        $(".accordion_head").click(function(){  
            //retrieveListItemsBelgium();
    
            if ($('.accordion_body').is(':visible')) {
                $(".accordion_body").slideUp(600);
                $(".plusminus").text('+');
            }
            $(this).next(".accordion_body").slideDown(600); 
            $(this).children(".plusminus").text('-');
        //alert("hello");
        });
    
    
    });
    
    function retrieveListItems_berba(country) {
        var clientContext = new SP.ClientContext.get_current();
        var oList = clientContext.get_web().get_lists().getByTitle('xyz');
    
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml("<View><Query><Where><Eq><FieldRef Name=\'Country\' /><Value Type=\'Choice\'>"+ country +"</Value></Eq></Where></Query>" + 
            '<RowLimit>100</RowLimit></View>'
        );
        this.collListItem = oList.getItems(camlQuery);
        clientContext.load(collListItem);
        clientContext.executeQueryAsync(
            Function.createDelegate(this, this.onQuerySucceeded), 
            Function.createDelegate(this, this.onQueryFailed)
        );
    }
    
    function onQuerySucceeded(sender, args) {
    
        var counter = 1;
        var LiTextCountry = '';
        var listItemInfo = '';
        var listItemEnumerator = collListItem.getEnumerator();
    
        while (listItemEnumerator.moveNext()) {
    
            var oListItem = listItemEnumerator.get_current();
            //alert("a"+counter);
            var ContactName= oListItem.get_item('ContactName')
            //alert("b"+counter);
            var Title= oListItem.get_item('Title'); 
            var Country= oListItem.get_item('Country');
            var image= oListItem.get_item('Image').get_url(); 
    
            var ULIDCountry= document.getElementById(Country+counter);
    
            LiTextCountry = "<div class='media-left'><img src='" + image + "' class='media-object' style='width:60px'></div>" +
                      "<div class='media-body'><h4 class='media-heading'>" + ContactName +"</h4><span>" + Title + "</span>"+
                      "</div>";
    
            //alert(String.raw`${LiTextCountry}`);
             ULIDCountry.innerHTML = LiTextCountry;
             counter++;
    
        }
    }
    
    function onQueryFailed(sender, args) {
        alert('Request failed. ' + args.get_message() + 
            '\n' + args.get_stackTrace());
    }
    
    
    </script>


    Thanks

    Paru


    Paru

    • Moved by Bill_Stewart Wednesday, December 13, 2017 5:13 PM Move to more appropriate forum
    Wednesday, December 13, 2017 12:42 PM

All replies