首页 > 解决方案 > 优化 SharePoint 列表检索

问题描述

下面的代码片段检索子站点中每个列表中的所有项目。然后按列表标题过滤,以便仅解析标题中包含“RDTEN”或“采购”的列表中的信息。有了最终结果,我需要为聚合信息构建一个图表。

我需要帮助优化此代码,使用更好的查询将缩短响应时间,因为我可能能够在查询本身中搜索 RegEx 项目,从而删除双 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)
 }

标签: restarraylistsharepointcsom

解决方案


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

/_api/search/query?querytext='(Title:RDTEN OR Title:procurement)'

关键字查询语言 (KQL) 语法参考


推荐阅读