将SPFX扩展部署到多个网站集

时间:2019-05-08 08:20:07

标签: 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")

1 个答案:

答案 0 :(得分:1)

根据您的情况,使用租户范围的部署并使用属性来控制扩展应在其上处于活动状态的站点可能更有意义:

  1. 在config / package-solution.json中将skipFeatureDeployment设置为true
  2. 还应在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"
          ]
        }
      }
    ]
  1. 在sharepoint /资产中创建ClientSideInstance.xml和elements.xml
  2. 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="{&quot;testMessage&quot;:&quot;Test message&quot;}">
    </ClientSideComponentInstance>
</Elements>
  1. 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="{&quot;testMessage&quot;:&quot;Test message&quot;}">
    </CustomAction>
</Elements>
  1. 在扩展的主文件(即src / extensions / myAppCust / MyAppCust.ts)中,在扩展的接口中添加属性allowedSites: string[];
export interface IAlertPopupApplicationCustomizerProperties {
  allowedSites: string[];
}
  1. 在有意义的扩展主文件中,仅当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;
  }
}
  1. 构建解决方案:gulp clean && gulp bundle && gulp package-solution --ship
  2. 将解决方案部署到应用目录。
  3. 访问“租户范围扩展名”列表(在应用程序目录网站上,访问网站内容,然后租户范围扩展名)。
  4. 在列表中选择新创建的条目(在App目录中部署时创建的条目),在功能区中选择“项目”选项卡,然后选择“编辑项目”。
  5. 在组件属性内,将allowedSites属性添加为数组,并将所有允许的站点(URL)添加到列表中,以确保该条目为有效的json)。
{
allowedSites: [
"https://contoso.sharepoint.com/sites/mySite"
],
testMessage: "Test message"
}

enter image description here