spfx - 将 SPFX 扩展部署到多个站点集合
问题描述
我已将 SPFX 扩展包部署到应用目录。我们需要将此应用程序部署到多个站点集合。我尝试了不工作的powershell,任何人都可以帮忙。
$credentials = Get-Credential
Connect-PnPOnline "URl of the site" -Credentials $credentials
$context = Get-PnPContext
$web = Get-PnPWeb
$context.Load($web)
Invoke-PnPQuery
$ca = $web.UserCustomActions.Add()
$ca.ClientSideComponentId = "2dbe5b9b-72f7-4dbf-bd6d-43e91ae3a7cc"
$ca.Location = "ClientSideExtension.ApplicationCustomizer"
$ca.Name = "reportanissue"
$ca.Title = "my-spfx-client-side-solution"
$ca.Description = "Deploys a custom action with ClientSideComponentId association"
$ca.Update()
$context.Load($web.UserCustomActions)
Invoke-PnPQuery
Write-Host $("deployed")
解决方案
根据您的情况,使用租户范围的部署并使用属性来控制扩展应该在哪些站点上处于活动状态可能更有意义:
- 在 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"
}
推荐阅读
- asp.net-core - 从 Entity Framework Core 中的 cookie 和基本路径创建数据库上下文
- javascript - 从资产文件夹打开本地 html 时,window.open() 弹出窗口被广告拦截器阻止,如果文件没有 .html 扩展名,则工作正常
- python - 如何将列表输入到函数中并在 Python 中接收列表?
- javascript - 指示 id 以确认 recatjs 中的操作时弹出错误
- c# - 显式等待不同的形式
- ruby-on-rails - Rails 指定所需的范围
- python - 我需要从python中的列表中提取每个字典吗?
- java - 如何在 Java 中打开 Excel 工作簿并删除所有只有标题行的工作表
- jquery - :active 伪类无法正常工作
- java - 有什么方法可以避免在 java map-struct 中使用原始字符串?