1

When i try to retrieve list items in sharepoint list using javascript i get the error my code

<script src="/_layouts/MicrosoftAjax.js" type="text/javascript"></script>
<script type="text/javascript" src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type='text/javascript' src='https://www.google.com/jsapi'></script>
<script type="text/javascript">
    google.load('visualization', '1', {packages:['orgchart']});
    google.setOnLoadCallback(onQuerySucceeded);
    var hostweburl = "http://zadbns004/";
    $(document).ready(function () {
        //hostweburl = "http://spdev:52001";
        // The js files are in a URL in the form:
        // web_url/_layouts/15/resource_file
        var scriptbase = hostweburl + "/_layouts/15/";
        // Load the js files and continue to
        // the execOperation function.
        $.getScript(scriptbase + "SP.Runtime.js",
            function () {
                $.getScript(scriptbase + "SP.js", execOperation);
            }
        );
    });
    // Function to execute basic operations.
    function execOperation() {
        retrieveListItems(hostweburl);
    }    

    function retrieveListItems(siteUrl) {
        var input = document.getItemById('siteDrop').value;
        var clientContext = new SP.ClientContext(siteUrl);
        var oList = clientContext.get_web().get_lists().getByTitle('Employees');            
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml(
            '<View><Query><Where><Geq><FieldRef Name=\'Site\'><Value Type=\'Text\'>'+input+'</Value></Geq></Where></Query></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 data = new google.visualization.DataTable();
        data.addColumn('string', 'Name');
        data.addColumn('string', 'Manager');
        data.addColumn('string', 'ToolTip');
        //data.addRows([['Bob', '', 'CEO'],['Carol', 'Bob', 'MD']]);


        var listItemInfo = '';
        var listItemEnumerator = collListItem.getEnumerator();

        var _name = '';
        var _manager = '';
        var _jobtitle = '';
        while (listItemEnumerator.moveNext()) {
            var oListItem = listItemEnumerator.get_current();            
            _name = oListItem.get_item('EmpFullName');
            _manager = oListItem.get_item('EmpManager')
            _jobtitle = oListItem.get_item('EmpJobTitle');
            if(_manager){
            data.addRow([_name, _manager,_jobtitle]);}
            listItemInfo += _name + " " + _manager +'\n';
        }
        //alert(listItemInfo.toString());
        var chart = new google.visualization.OrgChart(document.getElementById('chart_div'));
        chart.draw(data, {allowHtml:true});        
    }
    function onQueryFailed(sender, args) {
        //alert('Request failed. ' + args.get_message() + 
        //    '\n' + args.get_stackTrace());
    }     
</script>
<h1>Organisational Chart</h1>
<div id='chart_div'></div>
<select id='siteDrop>
<option Value='Item 1'>Item 1</option>
<option Value='Item 2'>Item 2</option>
<option Value='Item 3'>Item 3</option>
<option Value='Item 4'>Item 4</option>
</select>
<input type="button" value="Select" onclick="execOperation()">
4
  • What error are you getting? and at which line?
    – Aanchal
    Jun 23, 2014 at 9:44
  • in the query i am trying to get all the employees at a specific site, the retrieve the name, job title and manager from the list, but i get the error on line var listItemEnumerator = colllistItem.GetEnumerator;
    – Egor
    Jun 23, 2014 at 9:54
  • Hi can you try without setting viewXml condition and check if its working fine. Like camlQuery.set_viewXml('<View><RowLimit>100</RowLimit></View>');
    – Aanchal
    Jun 23, 2014 at 10:08
  • Still no luck after trying without query. Can you maybe help me understand why it brings back item 2 employees but every other option in the drop down brings back all the employees?
    – Egor
    Jun 23, 2014 at 12:19

1 Answer 1

2

Try declaring collListItem outside the scope of the function and removing the 'this' keyword from the line:

this.collListItem = oList.getItems(camlQuery);

For example:

var collListItem;
function retrieveListItems(siteUrl) {
        var input = document.getItemById('siteDrop').value;
        var clientContext = new SP.ClientContext(siteUrl);
        var oList = clientContext.get_web().get_lists().getByTitle('Employees');            
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml(
            '<View><Query><Where><Geq><FieldRef Name=\'Site\'><Value Type=\'Text\'>'+input+'</Value></Geq></Where></Query></View>'
        );
        collListItem = oList.getItems(camlQuery);            
        clientContext.load(collListItem);
        clientContext.executeQueryAsync(
            Function.createDelegate(this, this.onQuerySucceeded), 
            Function.createDelegate(this, this.onQueryFailed)
        ); 
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.