How To Deploy, Publish And Install SharePoint Framework Extension using PnP PowerShell

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.
  1. # Declare and Initialize Variables   
  2. $CurrentDirectory =  Split-Path -parent $MyInvocation.MyCommand.Definition  
  3. $Sites= Get-content "$CurrentDirectory\Sites.txt"  
  4. $appPath="$CurrentDirectory\site-redirection.sppkg"  
  5. $currentTime= $(get-date).ToString("yyyyMMddHHmmss")  
  6. $logFile = "$CurrentDirectory\$currentTime LogReport.csv"  
  7. add-content $logFile "URL,Status,exception"  
  8. $credentials=Get-Credential  
  9.   
  10. # Add App   
  11. function AddApp  
  12. {  
  13.       foreach ($site in $Sites)   
  14.       {  
  15.         try  
  16.         {  
  17.             Connect-PnPOnline -URL $site -Credentials $credentials   
  18.             # Add the app to the app catalog and publish it             
  19.             Add-PnPApp -Path $appPath -Scope Site -Publish                                 
  20.             Disconnect-PnPOnline  
  21.         }  
  22.         Catch  
  23.         {   
  24.             add-content $logFile  "$($site),No , $($_.Exception.Message)"   
  25.             Continue;  
  26.         }  
  27.       }  
  28. }  
  29.  
  30. # Install App   
  31. function InstallApp  
  32. {  
  33.       foreach ($site in $Sites)   
  34.       {  
  35.         try  
  36.         {  
  37.             Connect-PnPOnline -URL $site -Credentials $credentials  
  38.             # Get the app which is published in the site collection app catalog  
  39.             $app=Get-PnPApp -Scope Site | Where Title -EQ "site-redirection-client-side-solution"  
  40.             # Install the app at the root site  
  41.             Install-PnpApp -Identity $app.Id -scope site       
  42.              
  43.             # Get all the subsites and install the app    
  44.             $webColl=Get-PnPSubWebs -Recurse  
  45.             foreach($web in $webColl)  
  46.             {                    
  47.                   $connection= Connect-PnPOnline -URL $web.URL -Credentials $credentials                   
  48.                   Install-PnpApp -Identity $app.Id -scope site -Connection $connection  
  49.                   Disconnect-PnPOnline  
  50.             }        
  51.             Disconnect-PnPOnline  
  52.         }  
  53.         Catch  
  54.         {   
  55.             add-content $logFile  "$($site),No , $($_.Exception.Message)"   
  56.             Continue;  
  57.         }  
  58.       }  
  59. }    
  60.    
  61. # Call the functions    
  62. AddApp   
  63. InstallApp   
Open Windows PowerShell and navigate to the location where the file is placed.
 
Run the following command.
  1. .\ApplyRedirection.ps1  

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.