How to Enable Custom Script in SharePoint Online using PowerShell?
Requirement: Allow custom script in SharePoint Online!
By default, Custom scripts are disabled in SharePoint Online modern sites for security reasons. So, the below features are blocked when the custom scripting is disabled:
- Many web parts including content editor, script editor, etc are disabled.
- Uploading files that potentially include scripts are blocked
- SharePoint Designer script changes not allowed
- Save Site as Template, Save document library as template links disabled
- Solution Gallery, Sandbox Solutions, Theme Gallery, HTML Field Settings are not available
- All other script-related featured are turned off
So, To fix the above problems, we need to enable custom script settings. Here is how to enable custom script in SharePoint Online:
How to Enable Custom Script in SharePoint Online through Admin Center?
To allow custom script in SharePoint Online, follow these steps:
- Sign in to Office 365 and go to the SharePoint Online Admin Center: https://<tenant>-admin.sharepoint.com
- Click on Settings from the left navigation >> Scroll down to “Custom Script” section
- Under Custom Script section, Set “Allow users to run custom script on personal site” and “Allow
users to run custom script on self-service created sites” options. - Click on “OK” to save your changes.
This enables custom scripting in the SharePoint Online tenant. However, this change may take up to 24 hours to reflect. To enable scripting on a particular site collection immediately, use this PowerShell script through SharePoint Online Management Shell.
Also, when you enable custom scripting through the web interface, you enable it for all sites in the tenant. However, you can enable or disable custom scripting on a specific site collection through PowerShell.
Enable Custom Script in SharePoint Online using PowerShell
Here is how to enable custom script in SharePoint Online using PowerShell at the site collection level.
#Config Parameters
$AdminSiteURL="https://crescent-admin.sharepoint.com"
$SiteURL="https://crescent.sharepoint.com"
#Get Credentials to connect
$Cred = Get-Credential
#Connect to SharePoint Online Tenant Admin
Connect-SPOService -URL $AdminSiteURL -Credential $Cred
#sharepoint online enable custom scripts powershell - Disable DenyAddAndCustomizePages Flag
Set-SPOSite $SiteURL -DenyAddAndCustomizePages $False
To Disable Custom Script again, use:
Set-SPOsite $SiteURL -DenyAddAndCustomizePages $True
PnP PowerShell to Enable Custom Scripting in SharePoint Online
To enable custom script for a site collection, We’ve to set the “DenyAddandCustomizepages” flag using PowerShell in SharePoint Online:
#Parameters
$TenantAdminURL= "https://crescent-admin.sharepoint.com"
$SiteURL= "https://crescent.sharepoint.com/sites/Procurement"
#Connect to Tenant Admin
Connect-PnPOnline $TenantAdminURL -Interactive
#Get the Tenant Site Object
$Site = Get-PnPTenantSite -Url $SiteURL
#Enable Custom Scripting by turning OFF Deny Flag
$Site.DenyAddAndCustomizePages = "Disabled"
$Site.Update()
$Site.Context.ExecuteQuery()
Here is how to enable custom script in SharePoint online using PnP PowerShell Set-PnPSite cmdlet:
#Set Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/purchase"
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive
#sharepoint online allow custom scripts powershell
Set-PnPSite -Identity $SiteURL -NoScriptSite $false
How about enabling custom script for All SharePoint Online Sites in the tenant immediately?
#Parameters
$TenantURL = "https://Crescent.sharepoint.com"
#Get Credentials to connect
$Credential = Get-Credential
#Frame Tenant Admin URL from Tenant URL
$TenantAdminURL = $TenantURL.Insert($TenantURL.IndexOf("."),"-admin")
#Connect to Admin Center
Connect-PnPOnline -Url $TenantAdminURL -Credentials $Credential
#Get All Site collections - Filter BOT and MySite Host
$Sites = Get-PnPTenantSite -Filter "Url -like '$TenantURL' -and Url -notlike 'portals/hub'"
#Iterate through all sites
$Sites | ForEach-Object {
Write-host "Processing Site Collection:"$_.URL
#Enable Custom Script
If ($_.DenyAddAndCustomizePages -ne "Disabled")
{
$_.DenyAddAndCustomizePages = "Disabled"
$_.Update() | Out-Null
$_.Context.ExecuteQuery()
Write-host "`tCustom Script has been Enabled!" -f Green
}
Else
{
Write-host "`tCustom Script is Already Enabled!" -f Yellow
}
}
Thanks!
I believe you have to set flag as 0 or 1?
$False or $True also does the same!
I cannot find Custom Script section
You can get in through classic admin center: https://YOURTENANT-admin.sharepoint.com/_layouts/15/online/TenantSettings.aspx