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.
- #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.
- #Connect PNP online clear token cache
- #Import SharePoint PnP online module
- Import-Module SharePointPnPPowerShellOnline -Verbose
- #modify the admin URL according to your tenant
- $AdminUrl="https://contoso-admin.sharepoint.com"
- #modify the site url where the custom scripting needs to be enabled or disabled.
- $SiteUrl = "https://contoso.sharepoint.com/teams/mysitename"
- Connect-PnPOnline $AdminUrl -UseWebLogin
- $Context = Get-PnPContext
- $Status = $null
-
- do
- {
- Write-Host "Waiting... $Status"
- Start-Sleep -Seconds 10
- $Site=Get-PnPTenantSite -url $SiteUrl -Detailed
- $Status = $Site.Status
-
- } while ($Status -ne 'Active')
-
- $Site.DenyAddAndCustomizePages = "disabled"
- $Site.Update()
- $Context.ExecuteQuery()
- $Site=Get-PnPTenantSite -url $SiteUrl -Detailed
- $State = $Site.DenyAddAndCustomizePages
- Write-Host "Done...DenyAddAndCustomizePages: $State"
References
https://techcommunity.microsoft.com/t5/sharepoint-developer/how-do-i-set-denyaddandcustomizepages-using-pnp/m-p/32372