SPFx框架下用ListItemPicker获取SharePoint超100条列表项的方法咨询
一、获取全量列表项+列表ID的正确姿势
你提到的getAll()方法和获取列表ID并不冲突,只需把获取列表信息(含ID)与获取全量列表项的操作分开执行即可:
async fetchListAndItems() { await this.getConfiguration(); // 1. 先获取列表信息,拿到ID const targetList = await this.web.lists.getByTitle(lista_Protocollo).get(); this.setState({ listaProtocolloId: targetList.Id }); // 2. 调用getAll()自动处理分页,获取全量列表项 const allItems = await this.web.lists.getByTitle(lista_Protocollo) .items.orderBy("Created") .getAll(); // 处理全量列表项逻辑 // ... }
getAll()会自动递归请求SharePoint的分页接口(默认每页100条),直到获取所有列表项,无需手动处理分页逻辑。
如果想复用已拿到的列表ID优化请求,可改成以下写法:
const allItems = await this.web.lists.getById(targetList.Id) .items.orderBy("Created") .getAll();
二、ListItemPicker组件的全量数据配置
若你使用的是@pnp/spfx-controls-react中的ListItemPicker,默认存在分页限制,可通过以下方式配置获取全量选项:
- 自定义回调加载全量项
设置selectAllEnabled为true,同时通过getItemsCallback自定义数据获取逻辑,用getAll()替代默认分页查询:
<ListItemPicker context={this.context} listTitle={lista_Protocollo} selectAllEnabled={true} getItemsCallback={async (props) => { // 自定义获取全量项逻辑 const items = await this.web.lists.getByTitle(lista_Protocollo) .items.orderBy("Created") .getAll(); // 转换为组件要求的选项格式 return items.map(item => ({ key: item.Id.toString(), text: item.Title // 替换为你需要显示的字段 })); }} // 其他组件属性... />
- 调整单页加载数量(可选)
若无需全量但想一次性加载更多项,可设置pageSize属性(最大支持5000,受SharePoint列表视图阈值限制):
<ListItemPicker context={this.context} listTitle={lista_Protocollo} pageSize={5000} // 其他组件属性... />
注意事项
- 若列表项数量极大(超1万条),建议添加筛选条件缩小返回数据范围,避免性能问题。
- 当查询数据量超过SharePoint默认5000条视图阈值时,需确保查询字段已创建索引,否则会触发报错。
内容的提问来源于stack exchange,提问作者Marco
相关产品推荐
相关产品推荐

