I have 3 libraries and i would like to display the library items names. If I want to display the items of only one library, everything is ok, but if I'm looking for the items of two or more libraries, it still shows me only the Items of ONE library. What is wrong in my code?

    <p id="AInfo">library A Items</p>
    <p id="BInfo">library B Items</p>
    <p id="CInfo">library C Items</p>

    <script type="text/javascript">

        jQuery(document).ready(function(){ExecuteOrDelayUntilScriptLoaded(fill, "sp.js"); });

        var siteUrl = '/Example/test/';
        liblist = new Array("A", "B", "C"); //list of the libraries
        var i=0;
        var lib;

        function fill(){
            while (i<3){
                lib = liblist[i];
                alert(lib);
                retrieveListItemsInclude();
                i++;
            }
        }


        function retrieveListItemsInclude() {


            var clientContext = new SP.ClientContext(siteUrl);
            var oList = clientContext.get_web().get_lists().getByTitle(lib);

            var camlQuery = new SP.CamlQuery();
            camlQuery.set_viewXml('<View><RowLimit>100</RowLimit></View>');
            this.collListItem = oList.getItems(camlQuery);

            clientContext.load(collListItem, 'Include(Id, DisplayName, HasUniqueRoleAssignments)');

            clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
        }


            function onQuerySucceeded(sender, args) {
                var listItemInfo = '';
                var listItemEnumerator = collListItem.getEnumerator();            

                while (listItemEnumerator.moveNext()) {
                    var oListItem = listItemEnumerator.get_current();
                    listItemInfo += oListItem.get_displayName()+ '<br>';
                }

                var x = listItemInfo.toString();
                document.getElementById(lib+"Info").innerHTML = x;
        }


        function onQueryFailed(sender, args) {

            var x = 'Request failed. ' + args.get_message() + '\n' + args.get_stackTrace();
            document.getElementById(lib+"Info").innerHTML = x;  
        }

    </script>
share|improve this question
up vote 0 down vote accepted

I think you need to wait for the first request to be done before doing the second one. Something like that (but the code is dirty as you should avoid the global variables...)

<p id="AInfo">library A Items</p>
<p id="BInfo">library B Items</p>
<p id="CInfo">library C Items</p>

<script type="text/javascript">

    jQuery(document).ready(function(){ExecuteOrDelayUntilScriptLoaded(fill, "sp.js"); });

    var siteUrl = '/Example/test/';
    liblist = new Array("A", "B", "C"); //list of the libraries
    var i=0;
    var lib = liblist[i];
    retrieveListItemsInclude();

    function retrieveListItemsInclude() {


        var clientContext = new SP.ClientContext(siteUrl);
        var oList = clientContext.get_web().get_lists().getByTitle(lib);

        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml('<View><RowLimit>100</RowLimit></View>');
        this.collListItem = oList.getItems(camlQuery);

        clientContext.load(collListItem, 'Include(Id, DisplayName, HasUniqueRoleAssignments)');

        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }


        function onQuerySucceeded(sender, args) {
            var listItemInfo = '';
            var listItemEnumerator = collListItem.getEnumerator();            

            while (listItemEnumerator.moveNext()) {
                var oListItem = listItemEnumerator.get_current();
                listItemInfo += oListItem.get_displayName()+ '<br>';
            }

            var x = listItemInfo.toString();
            document.getElementById(lib+"Info").innerHTML = x;
            if (++i<3) {
              lib = liblist[i];
              retrieveListItemsInclude()
            }
    }


    function onQueryFailed(sender, args) {

        var x = 'Request failed. ' + args.get_message() + '\n' + args.get_stackTrace();
        document.getElementById(lib+"Info").innerHTML = x;  
    }

</script>
share|improve this answer
    
thank you for this idea! Now it works. I just need a quick demo solution, so dirty code is (at the moment) no problem :) – Dami Jul 10 '13 at 7:54

You can create an .ASHX web service and deploy it onto SharePoint Server. Then you can query it with an HttpRequest from JavaScript using REST and JSON.

http://msdn.microsoft.com/en-us/library/bb457204(v=office.12).aspx

share|improve this answer
    
Isn't there any possibility to just modify the code? – Dami Jul 9 '13 at 15:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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