Graph Toolkit SPFx PeoplePicker能否获取SharePoint组?
核心原因
MGT的PeoplePicker默认调用Microsoft Graph的/users或/groups端点,但SharePoint组不属于Graph标准组范畴(Graph组主要指M365组、安全组等),因此即便设置PersonType.any,也只会返回Graph可识别的主体,无法包含站点级的SharePoint组。
实现方案
方案1:自定义PeoplePicker数据源(推荐)
通过组件的peopleSearchQuery属性替换默认搜索逻辑,调用SharePoint REST API获取站点内的SharePoint组,再与Graph返回的结果合并,供组件展示。
步骤示例:
- 编写获取SharePoint组的方法:
private async getSharePointGroups(): Promise<any[]> { const siteUrl = this.context.pageContext.web.absoluteUrl; const apiUrl = `${siteUrl}/_api/web/sitegroups`; const response = await this.context.spHttpClient.get( apiUrl, SPHttpClient.configurations.v1, { headers: { 'Accept': 'application/json;odata=nometadata' } } ); const data = await response.json(); return data.value.map(group => ({ id: group.Id, displayName: group.Title, description: group.Description, entityType: 'group' })); }
- 自定义搜索逻辑并注入PeoplePicker:
import { PeoplePicker, PersonType } from '@microsoft/mgt-react'; import { useState, useEffect } from 'react'; // 组件内部逻辑 const [sharePointGroups, setSharePointGroups] = useState<any[]>([]); useEffect(() => { this.getSharePointGroups().then(groups => setSharePointGroups(groups)); }, []); const customSearch = async (query: string) => { // 获取Graph默认搜索结果 const graphResults = await PeoplePicker.searchPeople(query, { personType: PersonType.any }); // 过滤匹配的SharePoint组 const matchedGroups = sharePointGroups.filter(group => group.displayName.toLowerCase().includes(query.toLowerCase()) ); // 合并两类结果 return [...graphResults, ...matchedGroups]; }; // 渲染组件 <PeoplePicker type={PersonType.any} peopleSearchQuery={customSearch} selectionChanged={(selectedItems) => console.log(selectedItems)} />
方案2:配置权限
确保SPFx Web部件已获取足够权限调用SharePoint API:
- 在
package-solution.json中添加权限请求:
"webApiPermissionRequests": [ { "resource": "SharePoint", "scope": "Sites.Read.All" } ]
部署后需在SharePoint管理员中心批准该权限,否则无法读取站点组数据。
注意事项
- 若仅需当前站点的组,使用
/web/sitegroups端点;如需站点集级组,调整API路径为/sitegroups。 - 自定义返回结果需包含
id、displayName等核心字段,确保组件能正常渲染。
内容的提问来源于stack exchange,提问作者sugarQ
相关产品推荐
相关产品推荐

