根据您的情况,使用租户范围的部署并使用属性来控制扩展应该在哪些站点上处于活动状态可能更有意义:
- 在 config/package-solution.json 中将 skipFeatureDeployment 设置为 true
- 同样在 config/package-solution.json 中,一个条目应该被添加到特性中。如有必要,您可以使用guidgenerator 生成 guid:
"features": [
{
"title": "Application Extension - Deployment of custom action.",
"description": "Deploys a custom action with ClientSideComponentId association",
"id": "[any guid here]",
"version": "1.0.0.0",
"assets": {
"elementManifests": [
"elements.xml",
"clientsideinstance.xml"
]
}
}
]
- 在 sharepoint/assets 创建 ClientSideInstance.xml 和 elements.xml
- ClientSideInstance.xml 的格式应如下(ComponentId 应与其 manifest.json 文件中您的扩展程序的 id 匹配):
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<ClientSideComponentInstance
Title="MyAppCust"
Location="ClientSideExtension.ApplicationCustomizer"
ComponentId="917a86f2-15c1-403e-bbe1-59c6bd50d1e1"
Properties="{"testMessage":"Test message"}">
</ClientSideComponentInstance>
</Elements>
- elements.xml 的格式应如下(ComponentId 应与其 manifest.json 文件中您的扩展的 id 匹配):
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
Title="MyAppCust"
Location="ClientSideExtension.ApplicationCustomizer"
ComponentId="417feb7a-e193-4072-84b8-52ce58ed024f"
ClientSideComponentProperties="{"testMessage":"Test message"}">
</CustomAction>
</Elements>
- 在扩展的主文件(即 src/extensions/myAppCust/MyAppCust.ts)中,在扩展的接口中添加属性
allowedSites: string[];:
export interface IAlertPopupApplicationCustomizerProperties {
allowedSites: string[];
}
- 同样在扩展的主文件中,如果
this.context.pageContext.web.absoluteUrl 位于allowedSites 中,则仅允许功能。
即在 onInit() 如果当前站点不在允许的站点中,我会提前返回(在进行其他查询之前):
@override
public onInit(): Promise<void> {
const absoluteUri = this.context.pageContext.web.absoluteUrl;
// clean up allowed sites
const allowedSites = this.properties.allowedSitesRaw && this.properties.allowedSitesRaw.length
? this.properties.allowedSitesRaw.filter(item => !!item)
: [];
// return early if there are allowed sites specified and the current site is not allowed
if (allowedSites.length && allowedSites.indexOf(absoluteUri) == -1) {
return;
}
}
- 构建解决方案:
gulp clean && gulp bundle && gulp package-solution --ship
- 将解决方案部署到应用目录。
- 访问租户范围的扩展列表(在应用目录站点上,访问站点内容,然后是租户范围的扩展)。
- 在列表中选择新创建的条目(在应用目录中部署时创建),选择功能区中的“项目”选项卡,然后选择“编辑项目”。
- 在组件属性中,将 allowedSites 属性添加为数组,并将任何允许的站点 (Urls) 添加到列表中,确保条目是有效的 json)。
{
allowedSites: [
"https://contoso.sharepoint.com/sites/mySite"
],
testMessage: "Test message"
}