0

How do I get list item ( 4 list ) using the javascript object model? Relevant code as follows :

<script src="/JS/jquery-3.0.0.min.js" type="text/javascript"></script>
<script type="text/javascript">


$(document).ready(function () { ExecuteOrDelayUntilScriptLoaded(retrieveAllListProperties(), "sp.js"); });




function retrieveAllListProperties() {


var siteUrl="";
    var clientContext = new SP.ClientContext(siteUrl);
    var oWebsite = clientContext.get_web()
var oList = oWebsite.get_lists();
  var query = SP.CamlQuery.createAllItemsQuery();
  var allItems = oList.getItems(query);

clientContext.load(allItems,'Include(Title, status)');

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

function onQuerySucceeded() {

var listInfo = '';

var listEnumerator = allItems.getEnumerator();

while (listEnumerator.moveNext()) {
    var oList = listEnumerator.get_current();
    listInfo += 'Title: ' + oList.get_item('status') + '\n';
}
alert(listInfo);
}

  function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
</script>
  • You are not looping the oList object which holds all the SPLists. first load that and then do the oList.getItems() – Anand Jan 15 at 17:23
0

Sample code for your reference:

<script type="text/javascript">
    ExecuteOrDelayUntilScriptLoaded(getAllLists,'sp.js');
    function getAllLists()
    {
        var result = []; 
        var ctx = SP.ClientContext.get_current();
        var lists = ctx.get_web().get_lists();
        ctx.load(lists,"Include(Id,Title)");

        ctx.executeQueryAsync(
          function() {

               lists.get_data().forEach(function(list){
                   var items = list.getItems(SP.CamlQuery.createAllItemsQuery());
                   ctx.load(items,"Include(Id,FileRef)");
                   var listEntry = {
                     id: list.get_id().toString(),
                     title: list.get_title()
                   }
                   result.push({list: listEntry,
                                items: items}); 
               });
               ctx.executeQueryAsync(
               function() {
                   //transform listitem properties
                   result.forEach(function(item){

                       item.items = item.items.get_data().map(function(listItem){
                            return listItem.get_fieldValues(); 
                       });
                   }); 

                   console.log(JSON.stringify(result));
               },logError); 


          },logError);
     }
      function logError(sender,args){
       console.log(args.get_message());
    }
    </script>

Reference:

Get all lists and list items in a SharePoint site with JSOM

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.