SharePoint 提供商

使用 SharePoint Web 部件中的 SharePoint 提供程序为具有 Microsoft Graph 访问权限的组件提供支持。

若要详细了解身份验证提供程序,请参阅 提供程序

入门

在 Web 部件的 方法内 onInit() 初始化提供程序。 此示例使用 @microsoft/mgt-spfx

// import the providers at the top of the page
import {Providers, SharePointProvider} from '@microsoft/mgt-spfx';

// add the onInit() method if not already there in your web part class
protected async onInit() {
  Providers.globalProvider = new SharePointProvider(this.context);
}

现在,您可以在方法中添加 render() 任何组件,它将使用 SharePoint 上下文访问 Microsoft Graph。

public render(): void {
  this.domElement.innerHTML = `
    <mgt-agenda></mgt-agenda>
    `;
}

使用不同的云终结点

若要使用工具包呈现来自其他 Microsoft 365 终结点的数据,请使用此选项。

// import the providers at the top of the page
import {Providers, SharePointProvider} from '@microsoft/mgt-spfx';
import {GraphEndpoint} from '@microsoft/mgt-element';

const baseUrl: GraphEndpoint = 'https://graph.microsoft.us';

// add the onInit() method if not already there in your web part class
protected async onInit() {
  Providers.globalProvider = new SharePointProvider(this.context, baseUrl=baseUrl);
}

然后像往常一样使用工具包。

注意: Microsoft Graph 工具包需要 Typescript 3.7 或更高版本。 通过 安装正确的编译器,确保使用的是受支持的 Typescript 版本。

示例

有关如何初始化 SharePoint 提供程序的详细信息,请参阅 生成 SharePoint Web 部件 入门指南。

在工作台中测试

如果刚开始使用 SharePoint Web 部件,可以按照 生成第一个 Web 部件 指南进行操作。

创建 Web 部件并准备好使用这些组件后,需要确保 Web 部件具有访问 Microsoft Graph 的正确权限。 有关详细信息,请参阅在SharePoint 框架中使用 Microsoft Graph

简言之,请务必向 添加正确的权限 package-solution.json。 您需要将 Web 部件的包上传到 SharePoint,并让管理员批准请求的权限。

提示

生成 SharePoint Web 部件入门指南提供了有关配置和批准权限的分步说明。

注意: 如果不确定要添加哪些权限,则每个组件的文档都包含它所需的所有权限。