Introduction
In this blog, you will learn how to deploy and publish the SharePoint Framework Extension in site collection app catalog and how to install the app on the site using PnP PowerShell.
Note
I am working on migrating the sites from one tenant to another. Once the sites are migrated, I need to apply the redirection URL in the source site. To achieve this, I have created a SharePoint Framework Extension (Application Customizer) which is used to apply redirection to the Modern site. I have more than 200 sites in which this extension has to be deployed at site collection app catalog and need to be installed. In order to automate this, PnP PowerShell has been used.
Prerequisites
- Ensure SharePoint PnP PowerShell Online cmdlets are installed. Click here for more details on how to install.
- Ensure Site Collection App Catalog is enabled.
- Place the SPFx extension package in the same folder where the ps1 file is located.
- Create a text file named Sites.txt and add all the site URLs.
Steps Involved
Open Notepad.
Copy the below code and save the file as ApplyRedirection.ps1.
- # Declare and Initialize Variables
- $CurrentDirectory = Split-Path -parent $MyInvocation.MyCommand.Definition
- $Sites= Get-content "$CurrentDirectory\Sites.txt"
- $appPath="$CurrentDirectory\site-redirection.sppkg"
- $currentTime= $(get-date).ToString("yyyyMMddHHmmss")
- $logFile = "$CurrentDirectory\$currentTime LogReport.csv"
- add-content $logFile "URL,Status,exception"
- $credentials=Get-Credential
-
- # Add App
- function AddApp
- {
- foreach ($site in $Sites)
- {
- try
- {
- Connect-PnPOnline -URL $site -Credentials $credentials
- # Add the app to the app catalog and publish it
- Add-PnPApp -Path $appPath -Scope Site -Publish
- Disconnect-PnPOnline
- }
- Catch
- {
- add-content $logFile "$($site),No , $($_.Exception.Message)"
- Continue;
- }
- }
- }
-
- # Install App
- function InstallApp
- {
- foreach ($site in $Sites)
- {
- try
- {
- Connect-PnPOnline -URL $site -Credentials $credentials
- # Get the app which is published in the site collection app catalog
- $app=Get-PnPApp -Scope Site | Where Title -EQ "site-redirection-client-side-solution"
- # Install the app at the root site
- Install-PnpApp -Identity $app.Id -scope site
-
- # Get all the subsites and install the app
- $webColl=Get-PnPSubWebs -Recurse
- foreach($web in $webColl)
- {
- $connection= Connect-PnPOnline -URL $web.URL -Credentials $credentials
- Install-PnpApp -Identity $app.Id -scope site -Connection $connection
- Disconnect-PnPOnline
- }
- Disconnect-PnPOnline
- }
- Catch
- {
- add-content $logFile "$($site),No , $($_.Exception.Message)"
- Continue;
- }
- }
- }
-
- # Call the functions
- AddApp
- InstallApp
Open Windows PowerShell and navigate to the location where the file is placed.
Run the following command.
Summary
Thus, in this blog, you saw how to deploy and publish the SharePoint Framework Extension in site collection app catalog and how to install the app on the site using PnP PowerShell.