0

The below code snipped retrieves all items within each list in a subsite. Then filters by list title so that only information from lists containing "RDTEN" or "procurement" in their title get parsed. With the end result, I need to build a graph for the aggregated information.

I need help optimizing this code for a better response time, I attempted to load only the items for lists that match the regex in the title by moving the conditions around but that broke what I had and I am a novice at KQL/REST syntax. Can anyone help with the rewrite?

// Loads libraries from CDNs... 
javascript: ( function() {
    function l( u, i ) {
        var d = document;
        if ( !d.getElementById( i ) ) {
           var s = d.createElement( 'script' );
            s.src = u;
            s.id = i;
            s.async = true;
            d.body.appendChild( s );
         }
      }
    // l( url, idForElem )
    l( '//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js', 'msAjax' )
    l( '//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js', 'jQuery' )
} )();

// Wait for SP.js to finish loading before we run the queries.
SP.SOD.executeOrDelayUntilScriptLoaded( getSPListData, 'SP.js' );

function getSPListData() {
    // Set up the first query
    this.result = [];
     this.siteUrl = 
'https://sites/orgs/subsite';
     this.ctx = new SP.ClientContext( siteUrl )
    this.lists = ctx.get_web().get_lists();
    this.expr = /(RDTEN)|(procurement)/i 
    this.ctx.load( this.lists, "Include(Id,Title)" );

    ctx.executeQueryAsync( 
      this.getSPListData_SecondQuery.bind(this), 
      this.getSPListData_LogError.bind(this) 
    );

     }

function getSPListData_SecondQuery(sender, args){
   lists.get_data().forEach( function( list ) {
      // Set up the second query and push its results into the "Result" array
      var items = list.getItems( SP.CamlQuery.createAllItemsQuery() );
      ctx.load( items );
       // This entry is the list's base data
      var listEntry = {
           id: list.get_id().toString(),
            title: list.get_title()
      } 
      // Push the data into result, items will have all items in each list.
      result.push( {
           list: listEntry,
           items: items
      } );
   } );
    ctx.executeQueryAsync(
      this.getSPListData_SecondQuery_Success.bind(this),
      this.getSPListData_LogError.bind(this)
   );
}

function getSPListData_SecondQuery_Success(sender, args){
    //transform listitem properties. This is where the "items" section of "Result" is 
    // filled out.
   result.forEach( function( item ) {
       item.items = item.items.get_data().map( function( listItem ) {
         return listItem.get_fieldValues();
        } );
   } );

   // Filter each by the ReGex expression earlier on the Title field.
    var oListData = [];
    var itemTitle;
   for ( listNum = 0, listTot = result.length; listNum < listTot; listNum++ 
   ) {
   for ( itemNum = 0, itemTot = result[ listNum ].items.length; itemNum < 
   itemTot; itemNum++ ) {
    itemTitle = result[ listNum ].items[ itemNum ].Title
    if ( itemTitle && itemTitle.match( expr ) ) {
       // put data into a list
         oListData.push( result[ listNum ].items[ itemNum ] )
     }
     }
   }
  // make the data visible here
  manageData( oListData, result );
  }

// incase an error comes up in the execution of the queries
 function getSPListData_LogError( sender, args ) {
  console.log( args.get_message() ); 
}

function manageData( oListData, allData ) {
     // Do stuff with list items here...
   var oListDataField = document.getElementById( "listItemData" );
  var stringBuilding = '';

  for(i=0, itemMax = Math.min(5, oListData.length); i < itemMax; i++){
      stringBuilding += " Title = " + oListData[i].Title + "<br/>"
      stringBuilding += " Library = " + oListData[i].FileDirRef + "<br/> 
 <br/>"
  }
 oListDataField.innerHTML = stringBuilding

  // For seeing the relative data:
   console.log(allData)
   console.log(oListData)
 }

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.