rest - 优化 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)
}
解决方案
处理这种情况的更好方法是使用搜索 api,这样您将获得标题中包含您的关键字的每个列表项。
/_api/search/query?querytext='(Title:RDTEN OR Title:procurement)'
推荐阅读
- paraview - STL 表面上的点云,按元素集成
- regex - Apache Pig 中的正则表达式提取所需数据
- python - spacy 和 train 数据中的 Cased VS uncased BERT 模型
- file - Serilog 未写入从控制台 CORE 3 应用程序中的 appsettings.json 加载的配置的文件
- java - LeetCode 上的 Stock Span 问题是否有更好的解决方案?
- node.js - 使用 Lambda 自动化 NodeJS 应用程序(创建 json 文件)
- flutter - 最佳实践:在 GCF 内调用 Firebase 或在 GCF 之后再调用 Firebase
- c++ - 读取二进制文件直到结束 c++
- c++ - 是否在未初始化的变量上使用复合赋值运算符(+=,...)而不是 C++ 中的 UB?
- visual-studio-code - 尝试在终端中运行 powershell 命令时,F8 或“运行选择”在 VS Code 中不起作用