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. # Add App
  10. function AddApp
  11. {
  12. foreach ($site in $Sites)
  13. {
  14. try
  15. {
  16. Connect-PnPOnline -URL $site -Credentials $credentials
  17. # Add the app to the app catalog and publish it
  18. Add-PnPApp -Path $appPath -Scope Site -Publish
  19. Disconnect-PnPOnline
  20. }
  21. Catch
  22. {
  23. add-content $logFile "$($site),No , $($_.Exception.Message)"
  24. Continue;
  25. }
  26. }
  27. }
  28. # Install App
  29. function InstallApp
  30. {
  31. foreach ($site in $Sites)
  32. {
  33. try
  34. {
  35. Connect-PnPOnline -URL $site -Credentials $credentials
  36. # Get the app which is published in the site collection app catalog
  37. $app=Get-PnPApp -Scope Site | Where Title -EQ "site-redirection-client-side-solution"
  38. # Install the app at the root site
  39. Install-PnpApp -Identity $app.Id -scope site
  40. # Get all the subsites and install the app
  41. $webColl=Get-PnPSubWebs -Recurse
  42. foreach($web in $webColl)
  43. {
  44. $connection= Connect-PnPOnline -URL $web.URL -Credentials $credentials
  45. Install-PnpApp -Identity $app.Id -scope site -Connection $connection
  46. Disconnect-PnPOnline
  47. }
  48. Disconnect-PnPOnline
  49. }
  50. Catch
  51. {
  52. add-content $logFile "$($site),No , $($_.Exception.Message)"
  53. Continue;
  54. }
  55. }
  56. }
  57. # Call the functions
  58. AddApp
  59. 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.