SOLVED

How do I set DenyAddAndCustomizePages using PnP?

Brass Contributor

Hi

 

Is there a way I can set DenyAddAndCustomizePages using PnP?

 

I know it can be done using "SharePoint Online Management Shell" via the Set-SPOSite command. However, this command does not exist in PnP. Also I'd rather not have to load both modules. I'm trying to standardise on PnP.

 

Thanks.

7 Replies

Hi @Alan Trafford

 

I don't think that there is an option available for that imn PnP PowerShell

 

The only option is:

 

Connect-SPOService -Url https://mytenant-admin.sharepoint.com -Credential $cred

Set-SPOSite -Identity https://mytenant.sharepoint.com -DenyAddAndCustomizePages $true

 

PnP also doesn't have a Connect-SPOService equivalent Cmdlet. 

 

I had a look if there is an option with 

 

Get-PnPSite and then modify a propertty within the site object that is returned but there doesn't seem to be a property

 

When I ran Microsoft.Online.SharePoint.PowerShell\Get-SPOSite I did find the property.

 

$site = (Microsoft.Online.SharePoint.PowerShell\Get-SPOSite)[0]

$site.DenyAddAndCustomizePages 

 

This returned the value of DenyAddAndCustomizePages.

 

 

Hi @Pieter Veenstra

 

I've got it working with PnP, but with a small issue.

 

 

Clear-Host

Import-Module -Name SharePointPnPPowerShellOnline -DisableNameChecking

# Shorten name!
$DenyAddAndCustomizePagesStatusEnum = [Microsoft.Online.SharePoint.TenantAdministration.DenyAddAndCustomizePagesStatus]

Connect-PnPOnline -Url 'https://<private>-admin.sharepoint.com/' -Credentials (Get-Credentials)

$context = Get-PnPContext
$site = Get-PnPTenantSite -Detailed -Url 'https://<private>.sharepoint.com/'

$site.DenyAddAndCustomizePages = $DenyAddAndCustomizePagesStatusEnum::Disabled

$site.Update()
$context.ExecuteQuery()

Get-PnPTenantSite -Detailed -Url 'https://<private>.sharepoint.com/' | select url,DenyAddAndCustomizePages

Disconnect-PnPOnline

 

The above works, but if I run it twice within about 30 seconds, changing the value, I get the following error:

 

 

Exception calling "ExecuteQuery" with "0" argument(s): "Cannot set properties on site https://<private>.sharepoint.com/ 
because the site is not currently available."
At line:26 char:1
+ $context.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ServerException

 

This is becuase the previous update has not yet finished. I've tried putting a pause in using the following code (between the Update and ExecuteQuery), but it does not seem to get the updated value for the 'Status' field:

 

# Wait for site to become available
$status = $null
while ($status -ne 'Active')
{
    Write-Host "Waiting...   $status"
    Start-Sleep -Seconds 5
    $context.Load($site)
    $status = $site.Status
}

Unfortunately, the loop never exits. Any ideas?

 

Thanks.

 

 

 

 

Try below script

 

$status = $null
DO

{
 Write-Host "Waiting...   $status"
    Start-Sleep -Seconds 5
    $Site=Get-PnPTenantSite -url https://<Tenantname>.sharepoint.com/sites/contosobeta -Detailed
    $status = $Site.Status

} While ($status -ne 'Active')

 

Thanks @Alan Trafford.  How about on a site collection Get-PnPSite rather than Get-PnPTenantSite (using PnP)?  This site collection setting is available via SPO but seems to be unavailable on PnP.

@Alan Trafford

I have a similar request. And i am looking for a PnP script to enable custom script for one single site collection. Could you please further help with this?


@Jeff Border wrote:

Thanks @Alan Trafford.  How about on a site collection Get-PnPSite rather than Get-PnPTenantSite (using PnP)?  This site collection setting is available via SPO but seems to be unavailable on PnP.



@Jeff Border wrote:

Thanks @Alan Trafford.  How about on a site collection Get-PnPSite rather than Get-PnPTenantSite (using PnP)?  This site collection setting is available via SPO but seems to be unavailable on PnP.


 

best response confirmed by WestleyH (Microsoft)
Microsoft Verified Best Answer
Solution

@Alan Trafford This site will help you: Set-PnPTenantSite | PnP PowerShell
Set-PnPTenantSite -Identity "https://contoso.sharepoint.com/sites/sales" -DenyAddAndCustomizePages:$false

1 best response

Accepted Solutions
best response confirmed by WestleyH (Microsoft)
Microsoft Verified Best Answer
Solution

@Alan Trafford This site will help you: Set-PnPTenantSite | PnP PowerShell
Set-PnPTenantSite -Identity "https://contoso.sharepoint.com/sites/sales" -DenyAddAndCustomizePages:$false

View solution in original post