How To Enable Custom Scripting In SharePoint Online Site Collection

Introduction

 
You can enable and disable custom scripting in the SharePoint Online site collection. With the modern UI sites, MSFT by default disabled the custom scripting as the development is encouraged via SharePoint Framework (SPfx). For the classic sites by default, the custom scripting is turned on. 
 
Please note that we are using PnP PowerShell module to enable/disable custom scripting. This is based on the site property called 'DenyAddCustomizePages'. This is bit confusing. Points to note
 
To enable custom scripting on the site, the property 'DenyAddAndCustomizePages' should be disabled.
 
To disable custom scripting on the site, the property 'DenyAddAndCustomizePages' should be enabled.
 
Please note that using tenant settings the custom scripting for self-service sites is enabled / disabled for all. Using PnP PowerShell module, the custom scripting can be enabled or disabled on site by site basis. 
 

Steps

 
Step 1
 
Import the correct PnP module. The module name is SharePointPnPPowerShellOnline
 
Import-Module SharePointPnPPowerShellOnline -Verbose 
 
Step 2
 
Connect to admin tenant using PnP powershell module. The account should have either SharePoint admin or Global Admin rights. 
 
Connect-PnPOnline https://contoso-admin.sharepoint.com -UseWebLogin
 
Step 3
 
Get the SharePoint Online site where the custom scritping needs to be enabled. 
 
$SiteUrl = "https://contoso.sharepoint.com/teams/mysitename"
 
Step 4
 
Initialize the current status in a variable and set to null. 
 
$Status=$null
 
Step 5
 
Verify the script and run against your tenant / site properties. Below is the complete script.
  1. #To enable custom scripting on the site, the property 'DenyAddAndCustomizePages' should be disabled.    
  2. #To disable custom scripting on the site, the property 'DenyAddAndCustomizePages' should be enabled.    
  3. #Connect PNP online clear token cache     
  4. #Import SharePoint PnP online module    
  5. Import-Module SharePointPnPPowerShellOnline -Verbose    
  6. #modify the admin URL according to your tenant    
  7. $AdminUrl="https://contoso-admin.sharepoint.com"    
  8. #modify the site url where the custom scripting needs to be enabled or disabled.     
  9. $SiteUrl = "https://contoso.sharepoint.com/teams/mysitename"    
  10. Connect-PnPOnline $AdminUrl -UseWebLogin    
  11. $Context = Get-PnPContext    
  12. $Status = $null    
  13.     
  14. do    
  15. {    
  16.  Write-Host "Waiting...   $Status"    
  17.     Start-Sleep -Seconds 10    
  18.     $Site=Get-PnPTenantSite -url $SiteUrl -Detailed    
  19.     $Status = $Site.Status    
  20.     
  21. while ($Status -ne 'Active')    
  22.     
  23. $Site.DenyAddAndCustomizePages = "disabled"     
  24. $Site.Update()    
  25. $Context.ExecuteQuery()    
  26. $Site=Get-PnPTenantSite -url $SiteUrl -Detailed    
  27. $State = $Site.DenyAddAndCustomizePages    
  28. Write-Host "Done...DenyAddAndCustomizePages:  $State"    
References
 
https://techcommunity.microsoft.com/t5/sharepoint-developer/how-do-i-set-denyaddandcustomizepages-using-pnp/m-p/32372