Home

How do I set DenyAddAndCustomizePages using PnP?

%3CLINGO-SUB%20id%3D%22lingo-sub-32372%22%20slang%3D%22en-US%22%3EHow%20do%20I%20set%20DenyAddAndCustomizePages%20using%20PnP%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-32372%22%20slang%3D%22en-US%22%3E%3CP%3EHi%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIs%20there%20a%20way%20I%20can%20set%20DenyAddAndCustomizePages%20using%20PnP%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20know%20it%20can%20be%20done%20using%20%22SharePoint%20Online%20Management%20Shell%22%20via%20the%20Set-SPOSite%20command.%20However%2C%20this%20command%20does%20not%20exist%20in%20PnP.%20Also%20I'd%20rather%20not%20have%20to%20load%20both%20modules.%20I'm%20trying%20to%20standardise%20on%20PnP.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-250628%22%20slang%3D%22en-US%22%3ERe%3A%20How%20do%20I%20set%20DenyAddAndCustomizePages%20using%20PnP%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-250628%22%20slang%3D%22en-US%22%3E%3CP%3EThanks%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F25364%22%20target%3D%22_blank%22%3E%40Alan%20Trafford%3C%2FA%3E.%26nbsp%3B%20How%20about%20on%20a%20site%20collection%20Get-PnPSite%20rather%20than%20Get-PnPTenantSite%20(using%20PnP)%3F%26nbsp%3B%20This%20site%20collection%20setting%20is%20available%20via%20SPO%20but%20seems%20to%20be%20unavailable%20on%20PnP.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-32424%22%20slang%3D%22en-US%22%3ERe%3A%20How%20do%20I%20set%20DenyAddAndCustomizePages%20using%20PnP%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-32424%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F3058%22%20target%3D%22_blank%22%3E%40Manidurai%20Mohanamariappan%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThat%20worked!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-32416%22%20slang%3D%22en-US%22%3ERe%3A%20How%20do%20I%20set%20DenyAddAndCustomizePages%20using%20PnP%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-32416%22%20slang%3D%22en-US%22%3E%3CP%3ETry%20below%20script%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%24status%20%3D%20%24null%0ADO%0A%0A%7B%0A%20Write-Host%20%22Waiting...%20%20%20%24status%22%0A%20%20%20%20Start-Sleep%20-Seconds%205%0A%20%20%20%20%24Site%3DGet-PnPTenantSite%20-url%20https%3A%2F%2F%26lt%3BTenantname%26gt%3B.sharepoint.com%2Fsites%2Fcontosobeta%20-Detailed%0A%20%20%20%20%24status%20%3D%20%24Site.Status%0A%0A%7D%20While%20(%24status%20-ne%20'Active')%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-32394%22%20slang%3D%22en-US%22%3ERe%3A%20How%20do%20I%20set%20DenyAddAndCustomizePages%20using%20PnP%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-32394%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F2387%22%20target%3D%22_blank%22%3E%40Pieter%20Veenstra%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20got%20it%20working%20with%20PnP%2C%20but%20with%20a%20small%20issue.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EClear-Host%0A%0AImport-Module%20-Name%20SharePointPnPPowerShellOnline%20-DisableNameChecking%0A%0A%23%20Shorten%20name!%0A%24DenyAddAndCustomizePagesStatusEnum%20%3D%20%5BMicrosoft.Online.SharePoint.TenantAdministration.DenyAddAndCustomizePagesStatus%5D%0A%0AConnect-PnPOnline%20-Url%20'https%3A%2F%2F%26lt%3Bprivate%26gt%3B-admin.sharepoint.com%2F'%20-Credentials%20(Get-Credentials)%0A%0A%24context%20%3D%20Get-PnPContext%0A%24site%20%3D%20Get-PnPTenantSite%20-Detailed%20-Url%20'https%3A%2F%2F%26lt%3Bprivate%26gt%3B.sharepoint.com%2F'%0A%0A%24site.DenyAddAndCustomizePages%20%3D%20%24DenyAddAndCustomizePagesStatusEnum%3A%3ADisabled%0A%0A%24site.Update()%0A%24context.ExecuteQuery()%0A%0AGet-PnPTenantSite%20-Detailed%20-Url%20'https%3A%2F%2F%26lt%3Bprivate%26gt%3B.sharepoint.com%2F'%20%7C%20select%20url%2CDenyAddAndCustomizePages%0A%0ADisconnect-PnPOnline%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20above%20works%2C%20but%20if%20I%20run%20it%20twice%20within%20about%2030%20seconds%2C%20changing%20the%20value%2C%20I%20get%20the%20following%20error%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3EException%20calling%20%22ExecuteQuery%22%20with%20%220%22%20argument(s)%3A%20%22Cannot%20set%20properties%20on%20site%20https%3A%2F%2F%26lt%3Bprivate%26gt%3B.sharepoint.com%2F%20%0Abecause%20the%20site%20is%20not%20currently%20available.%22%0AAt%20line%3A26%20char%3A1%0A%2B%20%24context.ExecuteQuery()%0A%2B%20~~~~~~~~~~~~~~~~~~~~~~~%0A%20%20%20%20%2B%20CategoryInfo%20%20%20%20%20%20%20%20%20%20%3A%20NotSpecified%3A%20(%3A)%20%5B%5D%2C%20MethodInvocationException%0A%20%20%20%20%2B%20FullyQualifiedErrorId%20%3A%20ServerException%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20is%20becuase%20the%20previous%20update%20has%20not%20yet%20finished.%20I've%20tried%20putting%20a%20pause%20in%20using%20the%20following%20code%20(between%20the%20Update%20and%20ExecuteQuery)%2C%20but%20it%20does%20not%20seem%20to%20get%20the%20updated%20value%20for%20the%20'Status'%20field%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%23%20Wait%20for%20site%20to%20become%20available%0A%24status%20%3D%20%24null%0Awhile%20(%24status%20-ne%20'Active')%0A%7B%0A%20%20%20%20Write-Host%20%22Waiting...%20%20%20%24status%22%0A%20%20%20%20Start-Sleep%20-Seconds%205%0A%20%20%20%20%24context.Load(%24site)%0A%20%20%20%20%24status%20%3D%20%24site.Status%0A%7D%3C%2FPRE%3E%3CP%3EUnfortunately%2C%20the%20loop%20never%20exits.%20Any%20ideas%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-32375%22%20slang%3D%22en-US%22%3ERe%3A%20How%20do%20I%20set%20DenyAddAndCustomizePages%20using%20PnP%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-32375%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F25364%22%20target%3D%22_blank%22%3E%40Alan%20Trafford%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20don't%20think%20that%20there%20is%20an%20option%20available%20for%20that%20imn%20PnP%20PowerShell%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThe%20only%20option%20is%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EConnect-SPOService%20-Url%20%3CA%20href%3D%22https%3A%2F%2Fmytenant-admin.sharepoint.com%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fmytenant-admin.sharepoint.com%3C%2FA%3E%20-Credential%20%24cred%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3ESet-SPOSite%20-Identity%20%3CA%20href%3D%22https%3A%2F%2Fmytenant.sharepoint.com%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fmytenant.sharepoint.com%3C%2FA%3E%20-DenyAddAndCustomizePages%20%24true%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPnP%20also%20doesn't%20have%20a%20Connect-SPOService%20equivalent%20Cmdlet.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20had%20a%20look%20if%20there%20is%20an%20option%20with%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGet-PnPSite%20and%20then%20modify%20a%20propertty%20within%20the%20site%20object%20that%20is%20returned%20but%20there%20doesn't%20seem%20to%20be%20a%20property%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20I%20ran%26nbsp%3BMicrosoft.Online.SharePoint.PowerShell%5CGet-SPOSite%20I%20did%20find%20the%20property.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3E%24site%20%3D%20(Microsoft.Online.SharePoint.PowerShell%5CGet-SPOSite)%5B0%5D%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%3CSTRONG%3E%24site.DenyAddAndCustomizePages%26nbsp%3B%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20returned%20the%20value%20of%26nbsp%3BDenyAddAndCustomizePages.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-763336%22%20slang%3D%22en-US%22%3ERe%3A%20How%20do%20I%20set%20DenyAddAndCustomizePages%20using%20PnP%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-763336%22%20slang%3D%22en-US%22%3E%3CP%3E%40%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F25364%22%20target%3D%22_blank%22%3EAlan%20Trafford%3C%2FA%3E%3C%2FP%3E%3CP%3EI%20have%20a%20similar%20request.%20And%20i%20am%20looking%20for%20a%20PnP%20script%20to%20enable%20custom%20script%20for%20one%20single%20site%20collection.%20Could%20you%20please%20further%20help%20with%20this%3F%3C%2FP%3E%3CBLOCKQUOTE%3E%3CHR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F50851%22%20target%3D%22_blank%22%3E%40Jeff%20Border%3C%2FA%3E%26nbsp%3Bwrote%3A%3CBR%20%2F%3E%3CP%3EThanks%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F25364%22%20target%3D%22_blank%22%3E%40Alan%20Trafford%3C%2FA%3E.%26nbsp%3B%20How%20about%20on%20a%20site%20collection%20Get-PnPSite%20rather%20than%20Get-PnPTenantSite%20(using%20PnP)%3F%26nbsp%3B%20This%20site%20collection%20setting%20is%20available%20via%20SPO%20but%20seems%20to%20be%20unavailable%20on%20PnP.%3C%2FP%3E%3CHR%20%2F%3E%3C%2FBLOCKQUOTE%3E%3CBLOCKQUOTE%3E%3CHR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F50851%22%20target%3D%22_blank%22%3E%40Jeff%20Border%3C%2FA%3E%26nbsp%3Bwrote%3A%3CBR%20%2F%3E%3CP%3EThanks%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F25364%22%20target%3D%22_blank%22%3E%40Alan%20Trafford%3C%2FA%3E.%26nbsp%3B%20How%20about%20on%20a%20site%20collection%20Get-PnPSite%20rather%20than%20Get-PnPTenantSite%20(using%20PnP)%3F%26nbsp%3B%20This%20site%20collection%20setting%20is%20available%20via%20SPO%20but%20seems%20to%20be%20unavailable%20on%20PnP.%3C%2FP%3E%3CHR%20%2F%3E%3C%2FBLOCKQUOTE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Alan Trafford
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.

6 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.