优化SharePoint列表检索

时间:2019-01-11 13:43:03

标签: rest arraylist sharepoint csom

下面的代码片段检索了子站点中每个列表内的所有项目。然后按列表标题进行筛选,以便仅解析其标题中包含“ RDTEN”或“采购”的列表中的信息。最终结果是,我需要为汇总信息构建图形。

我需要帮助优化此代码,使用更好的查询可以缩短响应时间,因为我可以在查询本身中搜索RegEx项,从而消除了double for循环。但是,有可能在单个请求中完成所有这些操作吗?

// 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 ] )
     }
     }
   }
  // you can 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)
 }

1 个答案:

答案 0 :(得分:0)

处理这种情况的更好方法是使用搜索api,这样您将获得每个列表项,这些列表项的标题中包含关键字。

/ _ api / search / query?querytext ='(标题:RDTEN或标题:采购)'

Keyword Query Language (KQL) syntax reference